正確的方式寫行到文件?我習慣了print >>f, "hi there"然而,似乎print >>越來越不受歡迎了。什么是建議的方法來做上面的線?更新*關于所有這些答復"\n".這是通用的還是Unix特有的?呃,我該怎么做?"\r\n"在Windows上?
4 回答

小怪獸愛吃肉
TA貢獻1852條經驗 獲得超1個贊
print()
from __future__ import print_function # Only needed for Python 2print("hi there", file=f)
import
print()
f = open('myfile', 'w')f.write('hi there\n') # python will convert \n to os.linesepf.close() # you can omit in most cases as the destructor will call it
在輸出時,如果換行符為None,則為任意 '\n'
寫入的字符被轉換為系統(tǒng)默認行分隔符, os.linesep
..如果換行符是 ''
沒有翻譯。如果換行符是任何其他合法值,則 '\n'
所寫的字符被翻譯成給定的字符串。
添加回答
舉報
0/150
提交
取消