disable the automatic change from \r\n to \n in python
Set the newline
keyword argument to open()
to '\r\n'
, or perhaps to the empty string:
with open(filename, 'r', encoding='utf-8', newline='\r\n') as f:
This tells Python to only split lines on the \r\n
line terminator; \n
is left untouched in the output. If you set it to ''
instead, \n
is also seen as a line terminator but \r\n
is not translated to \n
.
From the open()
function documentation:
newline controls how universal newlines mode works (it only applies to text mode). It can be
None
,''
,'\n'
,'\r'
, and'\r\n'
. [...] If it is''
, universal newlines 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.
Bold emphasis mine.