相关疑难解决方法(0)

f.write vs print >> f

至少有两种方法可以在python中写入文件:

f = open(file, 'w')
f.write(string)
Run Code Online (Sandbox Code Playgroud)

要么

f = open(file, 'w')
print >> f, string     # in python 2
print(string, file=f)  # in python 3
Run Code Online (Sandbox Code Playgroud)

这两者有区别吗?或者是更多的Pythonic?我正在尝试写一堆HTML文件,所以我需要通过我的文件写一堆写/打印语句(但我不需要模板引擎).

python

37
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×1