|
|

楼主 |
发表于 2018-3-25 15:45:16
|
显示全部楼层
"Interactive" text files (files for which isatty() returns True)
use line buffering. Other text files use the policy described above
for binary files.
newline controls how universal newlines works (it only applies to text
mode). It can be None, '', '\n', '\r', and '\r\n'. It works as
follows:
* 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 newline 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.
* On output, 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.
查了好久,终于感觉明白了。
交互式文件使用行进行缓冲,即每行作为一个可迭代对象的元素。
使用open打开文件时,换行符=none,其=\n的作用,使用for循环,首先将其整个文件放入迭代器,然后遍历时以行为一个单位。
|
|