read()python 2和python 3之间的差异

时间:2021-08-18 18:26:41

With the following MWE:

使用以下MWE:

with open('a','w') as f:
    f.write('\r')
with open('a','r') as f:
    print(ord(f.read()))

I get the following output:

我得到以下输出:

$ python2 test.py 
13
$ python3 test.py 
10

Can you explain why? 13 is the expected decimal number of \r in ascii and UTF-8 as far as I can tell.

你能解释一下原因吗? 13是ascii和UTF-8的预期十进制数\ r \ n据我所知。

1 个解决方案

#1


7  

Python 3's open defaults to universal newlines mode (newline=None), while Python 2's open only enables universal newlines mode if the mode string include U.

Python 3的open默认为通用换行模式(newline = None),而Python 2的open仅在模式字符串包含U时启用通用换行模式。

In universal newlines mode, a sequence \r (old Mac), \n (UNIX) or \r\n (DOS/Windows) are all recognized as newlines, and automatically converted to \n so line endings have a consistent representation to simplify string manipulation.

在通用换行模式下,序列\ r \ n(旧Mac),\ n(UNIX)或\ r \ n(DOS / Windows)都被识别为换行符,并自动转换为\ n,因此行结尾具有一致的表示形式以简化字符串操作。

If you want universal newlines in Python 2, you can either use the mode string to enable it or use io.open, which is a near exact equivalent of Python 3's built-in open (io.open on Python 3 is just another way to say open).

如果你想在Python 2中使用通用换行符,你可以使用模式字符串来启用它,或者使用io.open,它几乎完全等同于Python 3的内置开放(Python 3上的io.open只是另一种方式说开放)。

If you want to disable universal newlines handling on Python 3, pass open an argument of newline='' (for universal recognition for the purposes of breaking lines when reading/iterating, but no translation of line endings) or newline='\n' (for example) to mean only \n is recognized as a line ending at all, and again, no translation of line endings is performed. Passing newline='' is required to properly handle certain file formats; the csv module performs its own line ending handling, and newline='' ensures no information is lost before it reaches the csv reader.

如果要在Python 3上禁用通用换行符处理,请传递newline =''的参数(用于通用识别,以便在读取/迭代时断行,但不转换行结尾)或换行='\ n' (例如)仅表示\ n被识别为完全结束的行,并且再次不执行行结尾的转换。要正确处理某些文件格式,需要传递newline =''; csv模块执行自己的行结束处理,newline =''确保在到达csv阅读器之前没有信息丢失。

#1


7  

Python 3's open defaults to universal newlines mode (newline=None), while Python 2's open only enables universal newlines mode if the mode string include U.

Python 3的open默认为通用换行模式(newline = None),而Python 2的open仅在模式字符串包含U时启用通用换行模式。

In universal newlines mode, a sequence \r (old Mac), \n (UNIX) or \r\n (DOS/Windows) are all recognized as newlines, and automatically converted to \n so line endings have a consistent representation to simplify string manipulation.

在通用换行模式下,序列\ r \ n(旧Mac),\ n(UNIX)或\ r \ n(DOS / Windows)都被识别为换行符,并自动转换为\ n,因此行结尾具有一致的表示形式以简化字符串操作。

If you want universal newlines in Python 2, you can either use the mode string to enable it or use io.open, which is a near exact equivalent of Python 3's built-in open (io.open on Python 3 is just another way to say open).

如果你想在Python 2中使用通用换行符,你可以使用模式字符串来启用它,或者使用io.open,它几乎完全等同于Python 3的内置开放(Python 3上的io.open只是另一种方式说开放)。

If you want to disable universal newlines handling on Python 3, pass open an argument of newline='' (for universal recognition for the purposes of breaking lines when reading/iterating, but no translation of line endings) or newline='\n' (for example) to mean only \n is recognized as a line ending at all, and again, no translation of line endings is performed. Passing newline='' is required to properly handle certain file formats; the csv module performs its own line ending handling, and newline='' ensures no information is lost before it reaches the csv reader.

如果要在Python 3上禁用通用换行符处理,请传递newline =''的参数(用于通用识别,以便在读取/迭代时断行,但不转换行结尾)或换行='\ n' (例如)仅表示\ n被识别为完全结束的行,并且再次不执行行结尾的转换。要正确处理某些文件格式,需要传递newline =''; csv模块执行自己的行结束处理,newline =''确保在到达csv阅读器之前没有信息丢失。