源文件内容如下:
源文件每行后面都有回车,所以用下面输出时,中间会多了一行
>>> try:
with open(r"C:\Users\Administrator\Desktop\20190506biji0.txt" ) as f :
for line in f:
print(line)
except FileNotFoundError:
print("读取文件出错")
hello world
how are you?
I am fine
有两种方法处理:
1.print后面带 end=’’,表示不换行
>>> try:
with open(r"C:\Users\Administrator\Desktop\20190506biji0.txt" ) as f :
for line in f:
print(line,end='')
except FileNotFoundError:
print("读取文件出错")
hello world
how are you?
I am fine
2.用strip()函数去掉每一行的换行符
>>> try:
with open(r"C:\Users\Administrator\Desktop\20190506biji0.txt" ) as f :
for line in f:
line