python print语句打印换行符(尽管有逗号)

python print语句打印换行符(尽管有逗号),python,Python,“2.6.5(r265:79063,2010年4月16日,13:57:41)\n[GCC 4.4.3]” 我有这个 #! /usr/bin/env python f = open('filetest', 'w') f.write("This is a line") f.close() f = open('filetest', 'r') for i in f.readlines(): print i, 这将按如下方式打印o/p: $ ./filetest.py This is

“2.6.5(r265:79063,2010年4月16日,13:57:41)\n[GCC 4.4.3]”

我有这个

#! /usr/bin/env python

f = open('filetest', 'w')
f.write("This is a line")

f.close()

f = open('filetest', 'r')


for i in f.readlines():
    print i,
这将按如下方式打印o/p:

$ ./filetest.py 
This is a line
abc@abc-ubuntu:~/pythonpractice$
我想知道为什么在打印“This is a line”之后会出现换行提示? 因为
cat filestest
给出了:

$ cat filetest
This is a lineabc@abc-ubuntu:~/pythonpractice$ 

这是标准行为,阿飞。您可以改用sys.output.write,也可以 设置sys.output.softspace=False以防止换行

有关更多详细信息,请参阅本文:

或者您也可以使用:

#! /usr/bin/env python
from __future__ import print_function

with open('filetest', 'w') as f1:
    f1.write("This is a line")

with open('filetest', 'r') as f2:
    for line in f2.readlines():
        print(line, end='')
在处理文件时,尽量使用“with”,这样会更干净。
from __future__ import print_function

for line in f:
    print(line, end="")