当我在 Windows 上运行的 python 2.7 脚本中写入文本文件时,新行分隔符是 '\r\n'
,但我希望它是 '\n'
.
我试过使用open
使用 newline='\n'
,但它引发了异常。
import io
f= io.open("myfile.txt", "w", newline="\n")
f.write(”aaaaaaa”)
f.close()
最佳答案
以下对我有用,使用 \n
而不是 \r\n
import io
f= io.open("myfile.txt", "w", newline="\n")
#note the io module requires you to write unicode
f.write(unicode("asdasd\nasdasasd\n"))
f.close()
关于 python 2.7 : How to constrain the delimiter of a new line to be '\n' on Windows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14753894/