Python,按原样读取CRLF文本文件,使用CRLF

2024-06-16 14:40:44 发布

您现在位置:Python中文网/ 问答频道 /正文

with open(fn, 'rt') as f:
    lines = f.readlines()

读取带有LF行尾的crlf文本文件(WinXP,Py 2.6)。所以lines包含'\n'结尾。如何按原样获取线条:

  • 对于CRLF文件,获取以“\n\r”结尾的行
  • 对于LF文件,获取以'\n'结尾的行

Tags: 文件pyaswith结尾openfnlines
1条回答
网友
1楼 · 发布于 2024-06-16 14:40:44

使用^{},而不是内置的open()函数。这使您可以更好地控制如何使用newline参数处理换行:

import io

with io.open(fn, 'rt', newline='') as f:
    lines = f.readlines()

newline设置为空字符串时,将启用通用换行符支持,但返回未翻译的换行符;仍可以使用.readlines()查找以任何合法换行符结尾的行,但返回的数据正是文件中的数据:

On input, 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.

强调我的。

这与以二进制模式打开文件不同,在二进制模式下,.readlines()将只在\n字符上拆分文件。对于具有\r行尾或混合行尾的文件,这意味着行将无法正确拆分。

演示:

>>> import io
>>> open('test.txt', 'wb').write('One\nTwo\rThree\r\n')
>>> open('test.txt', 'rb').readlines()
['One\n', 'Two\rThree\r\n']
>>> io.open('test.txt', 'r', newline='').readlines()
[u'One\n', u'Two\r', u'Three\r\n']

注意,io.open()还将文件内容解码为unicode值。

相关问题 更多 >