python 2.7 : How to constrain the delimiter of a new line to be '\n' on Windows?

标签 python windows python-2.7

当我在 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/

相关文章:

python - 如何在matplotlib barh图中删除空的 "padding"?

python - 灰度图像中缺陷区域的分割,对阴影具有不变性

python - 在python中用新标签替换标签

python - 如何将某物称为对象而不是列表?

python - Pandas :从另一个数据框中逐列相乘?

windows - 数据库/Web 服务器作为虚拟机还是裸机?

c++ - 0's coming out as -85993460 instead of 0' 的数组

windows - 在 Windows 上集成 Vim 和 Git

python - 将数据写入 csv 文件时出现编码错误

json - Python : Interesting json. 转储序列化怪癖