【发布时间】:2021-01-20 03:32:19
【问题描述】:
我运行了以下代码,
with open('test.txt', 'r') as f:
for line in f:
print(line, end=' ')
我希望得到,
This is the first line This is the second line This is the third line
作为输出。
相反,我得到了,
This is the first line
This is the second line
This is the third line
谁能告诉我为什么会出现这种行为?
.txt文件内容如下,
This is the first line
This is the second line
This is the third line
【问题讨论】:
-
end = ' '您在打印的末尾添加一个空格。因此,您的脚本会读取以 \n 结尾的行,并添加一个空格。
标签: python python-3.x text printing file-io