Windows 版 Python 是否会插入 '\r\n'当被告知插入“\n”时?
我在家使用 PC,在工作使用 Mac。我从来没有遇到过 python 脚本或其输出中的换行符问题,但是每当我向老板发送某些内容时,我都会收到一封关于 Windows 换行符的愤怒电子邮件。
最近的一个是 python 脚本的输出,我告诉它以 '\n' 结束每一行,但仔细检查(在我工作的 Mac 上),似乎每一行实际上都以 '\r 结尾\n'。
发生了什么事,我该如何阻止它?我曾经在家里的 Linux 虚拟机中运行所有脚本,但我发现这太慢而且太繁琐,肯定有更简单的修复方法吗?
I use a PC at home and a Mac at work. I've never had any problems with line breaks in python script or their outputs, but whenever i send something to my boss i get an angry e-mail back about windows line breaks in it.
The most recent was the output of a python script where i'd told it to end every line with '\n', but on closer inspection (on my Mac at work) it seems that each line did in fact end with '\r\n'.
What's going on, and how do i stop it? I used to run all my scripts in a Linux virtual machine at home, but i found that was too slow and fiddly, surely there's a simpler fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您以文本模式打开了文件,并且 Python 正在根据您使用的平台规范换行符(Windows 使用
\r\n
而 Linux 仅使用\n)。您需要像这样以二进制模式打开文件:
当您读入文件时,它会执行相反的操作(
\r\n
将被\n
替换),除非您还可以指定二进制模式:This is because you have files opened in text mode and Python is normalizing the newlines in accordance with the platform you're using (Windows used
\r\n
and Linux just uses\n
). You need to open files in binary mode like this:It does the same thing in reverse when you read in files (
\r\n
will be replaced by\n
) unless you also specify binary mode:您看到的行为不是特定于 python 的。它来自 C 标准库中的缓冲文件处理函数,该函数是 Python 和其他高级语言的基础。除非被告知不这样做,否则它会在写入时将换行符转换为当前平台的本机文本文件换行符序列,并在读取时执行相反的操作。请参阅
fopen()< 的文档/code>
在您的本地系统上了解详细信息。在 Windows 上,这意味着 \n 在写入时将转换为 \r\n。
python 文档提到了换行符转换和其他
open()
模式选项 here 。一种解决方案是在第一个文件中打开输出文件时使用
open("filename", "wb")
而不是open("filename", "w")
地方。这将避免自动换行转换。只要你的老板使用某种形式的unix(包括OSX),它就应该为你的老板解决问题。不幸的是,这也意味着某些 Windows 文本编辑器(例如记事本?)会以奇怪的方式显示您的文件:另一种方法是根据需要转换您的文件,然后再将其发送给不使用 Windows 的人。为此目的存在各种转换程序,例如
dos2unix
和flip
。The behavior you are seeing is not python-specific. It comes from the buffered file-handling functions in the C standard library that underlies python and other high level languages. Unless told not to, it will convert newline characters to the current platform's native text file line break sequence when writing, and do the reverse when reading. See the documentation for
fopen()
on your local system for details. On Windows, it means \n will be converted to \r\n on writes.The python docs mention newline conversion and other
open()
mode options here.One solution would be to use
open("filename", "wb")
instead ofopen("filename", "w")
when opening the output file in the first place. That will avoid the automatic newline conversion. It ought to solve the problem for your boss, so long as your boss is using some form of unix (including OSX). Unfortunately, it will also mean that some Windows text editors (e.g. notepad?) will present your file strangely:Another approach would be to convert your files as needed before sending them to someone who doesn't use Windows. Various conversion programs exist for this purpose, such as
dos2unix
andflip
.