在 Mac 与 Windows 上处理 Python 中的 \r\n 与 \n 换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4599936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:29:20  来源:igfitidea点击:

Handling \r\n vs \n newlines in python on Mac vs Windows

pythonwindowsmacospython-2.x

提问by wrongusername

I have a python script that gave different output when run on a Windows machine and when run on a Mac. On digging deeper, I discovered that it was because when Python read in line breaks on the Mac (from a file), it read in \r\n, while somehow in Windows the \rdisappears.

我有一个 python 脚本,它在 Windows 机器上运行和在 Mac 上运行时给出不同的输出。深入挖掘后,我发现这是因为当 Python 在 Mac 上(从文件)读入换行符时,它读入了\r\n,而在 Windows 中不知何故\r消失了。

Thus, if I change every \nin the script to \r\n, it works fine on the Mac. But if I do that, it stops working on the Windows PC.

因此,如果我\n将脚本中的 each更改为\r\n,它在 Mac 上运行良好。但是,如果我这样做,它会在 Windows PC 上停止工作。

Is there an easy way to fix this problem?

有没有简单的方法来解决这个问题?

采纳答案by jonesy

I guess it may depend on what you're reading from, but the built-in open() function takes a 'mode' parameter, and if you pass 'U' for the mode, Python will take care of the newlines in a cross-platform way transparently. It requires that Python be built with universal newline support, but test it out!

我想这可能取决于您正在阅读的内容,但是内置的 open() 函数采用“模式”参数,如果您为模式传递“U”,Python 将处理交叉中的换行符-平台方式透明。它要求使用通用换行支持构建 Python,但请测试一下!

http://docs.python.org/library/functions.html#open

http://docs.python.org/library/functions.html#open

回答by Lennart Regebro

Different platforms have different codes for "new line". Windows have \r\n, Unix has \n, Old macs have \r and yes there are some systems that have \n\r too.

不同的平台有不同的“换行”代码。Windows 有 \r\n,Unix 有 \n,旧 mac 有 \r,是的,有些系统也有 \n\r。

When you open a file in text mode in Python 3, it will convert all newlines to '\n' and be done with it.

当您在 Python 3 中以文本模式打开文件时,它会将所有换行符转换为 '\n' 并完成它。

infile = open("filename", 'r')

Text mode is default, so if you say nothing, it's text mode. But it's always better to be explicit:

文本模式是默认的,所以如果你什么都不说,那就是文本模式。但最好是明确的:

infile = open("filename", 'rt')

If you don't want the translation of line endings to happen, open the file in binary mode:

如果您不希望发生行尾转换,请以二进制模式打开文件:

infile = open("filename", 'rb')

In Python 2 it's different. There this conversion would only happen by default on Windows. If you wanted it to happen on other platforms, you could add the universal newline flag:

在 Python 2 中它是不同的。默认情况下,这种转换只会在 Windows 上发生。如果您希望它发生在其他平台上,您可以添加通用换行符标志:

infile = open("filename", 'rU')

However, you say that you are on Python 3, and there it happens in text mode on allplatforms, so adding the U flag should make no difference.

但是,您说您使用的是 Python 3,并且它在所有平台上以文本模式发生,因此添加 U 标志应该没有区别。

回答by Felix

In Python 3, use the keyword argument "newline='\n'" in open() to use a specified line delimiter when writing text files. For more information, please see:

在 Python 3 中,在 open() 中使用关键字参数 "newline='\n'" 以在写入文本文件时使用指定的行分隔符。有关更多信息,请参阅:

https://pythonconquerstheuniverse.wordpress.com/2011/05/08/newline-conversion-in-python-3/

https://pythonconquerstheuniverse.wordpress.com/2011/05/08/newline-conversion-in-python-3/

http://docs.python.org/3/library/functions.html#open

http://docs.python.org/3/library/functions.html#open

回答by as g

On windows, they both are working fine if i try writing a file with either of the two(\r or \n) python interprets it as a line break in both the cases. While using "\r\n", it is interpreted as a double line break.(Python 3 on windows)

在 Windows 上,如果我尝试使用这两个(\r 或 \n)中的任何一个编写文件,它们都可以正常工作,python 在这两种情况下都将其解释为换行符。使用“\r\n”时,它被解释为双换行符。(Windows 上的 Python 3)

回答by jdhao

In Python 3, the Open()method has a newlineparameter:

在 Python 3 中,该Open()方法有一个newline参数:

newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

换行符控制通用换行符模式的工作方式(它仅适用于文本模式)。它可以是 None、''、'\n'、'\r' 和 '\r\n'。它的工作原理如下:

从流中读取输入时,如果换行符为 None,则启用通用换行符模式。输入中的行可以以 '\n'、'\r' 或 '\r\n' 结尾,这些在返回给调用者之前会被转换为 '\n'。如果是 '',则启用通用换行符模式,但行尾会返回给调用者未翻译。如果它具有任何其他合法值,则输入行仅由给定的字符串终止,并且行尾未翻译地返回给调用者。

将输出写入流时,如果换行符为 None,则写入的任何 '\n' 字符将转换为系统默认行分隔符 os.linesep。如果换行符是 '' 或 '\n',则不进行转换。如果换行符是任何其他合法值,则写入的任何 '\n' 字符都将转换为给定的字符串。

The old way of using Umode specifier has been deprecated in favor of this new way.

使用U模式说明符的旧方式已被弃用,取而代之的是这种新方式。

'U' universal newlines mode (deprecated)

'U' 通用换行符模式(已弃用)