您应该做的是以通用新线支持打开文件( Python 2.x ) 。 这是使用“ U” 或“ rU” 模式完成的。 任何类型的新线都会得到支持。 以下文件载于 Python 手册< a href=' http://docs. python.org/library/works. html#open” rel = “ noreferrer” > http://docs. python.org/library/ offices.html#open :
In addition to the standard fopen() values mode may be U or rU . Python is usually built with universal newline support; supplying U opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention
, the Macintosh convention
, or the Windows convention
. All of these external representations are seen as
by the Python program. If Python is built without universal newline support a mode with U is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen),
,
,
, or a tuple containing all the newline types seen.
对 Python 3 来说,有一个可以打开控制新线行为的新线选项。 查看文档, 似乎通用新线支持是默认的 。