python 如何打开文件按行读取,并去掉末尾换行符

时间:2021-12-07 07:07:40

1.按行读取

for line in open("aa.fasta"):
    print line

2. 去掉末尾换行符

line = line.strip('\n')

3. 按行读取方法二

f = open("aa.fasta")              
line = f.readline()                
while line: 
    print line,                      
    line = f.readline() 
f.close()  

print line加逗号,可以忽略输出的换行符【python2】,在python3中可以写为

print(line, end = '')