我的代码包含以下声明,可以正常使用
with open(fname, 'w') as f:
print >> f, result
此处result
不是字符串,而是一些自定义对象。
现在我将其更改为
with open(fname, 'w') as f:
f.write(str(result))
它基本上有效,但最后还有一个空行。是否有一种清除方法可以摆脱它,甚至不首先产生它?
答案 0 :(得分:0)
为了能够在python 2中使用print
,没有换行符和文件重定向,你可以使用print
导入python 3 __futures__
函数,并享受新的参数这个功能:
from __future__ import print_function
with open("U:/foo.txt","w") as f:
print("a",end="",file=f)
的引用: