Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read() differences between python 2 and python 3

With the following 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.

like image 850
noleti Avatar asked Oct 26 '25 08:10

noleti


1 Answers

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.

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.

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).

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.

like image 163
ShadowRanger Avatar answered Oct 27 '25 21:10

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!