Line endings in python
Yes, it's a python thing; by default open()
opens files in text mode, where line endings are translated depending on what platform your code is running on. You'll have set newline=''
in the open()
call to ask for line endings to be passed through unaltered.
Python 2's standard open()
function doesn't support this option, and only opening in binary mode would prevent the translation, but you can use the Python 3 behaviour by using io.open()
instead.
From the documentation on open
:
newline
controls how universal newlines mode works (it only applies to text mode).[...]
- When reading input from the stream, if
newline
isNone
, 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.
Opening the file in binary mode will avoid this in Py2 on Windows. However, in Py3 (and in Py2.6+ if you use io.open
instead of the builtin), binary mode vs text mode means something well defined and platform independent, and doesn't affect universal newlines. Instead, you can do:
file = open(filename, 'r', newline='')
And newlines won't be normalised.