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.
使用^{} ,而不是内置的
open()
函数。这使您可以更好地控制如何使用newline
参数处理换行:将
newline
设置为空字符串时,将启用通用换行符支持,但返回未翻译的换行符;仍可以使用.readlines()
查找以任何合法换行符结尾的行,但返回的数据正是文件中的数据:强调我的。
这与以二进制模式打开文件不同,在二进制模式下,
.readlines()
将只在\n
字符上拆分文件。对于具有\r
行尾或混合行尾的文件,这意味着行将无法正确拆分。演示:
注意,
io.open()
还将文件内容解码为unicode值。相关问题 更多 >
编程相关推荐