disable the automatic change from rn to n in python

Question:

I am working under ubuntu on a python3.4 script where I take in parameter a file (encoded to UTF-8), generated under Windows. I have to go through the file line by line (separated by rn) knowing that the "lines" contain some 'n' that I want to keep.

My problem is that Python transforms the file’s "rn" to "n" when opening. I’ve tried to open with different modes ("r", "rt", "rU").

The only solution I found is to work in binary mode and not text mode, opening with the "rb" mode.

Is there a way to do it without working in binary mode or a proper way to do it?

Asked By: lu1her

||

Answers:

Set the newline keyword argument to open() to 'rn', or perhaps to the empty string:

with open(filename, 'r', encoding='utf-8', newline='rn') as f:

This tells Python to only split lines on the rn line terminator; n is left untouched in the output. If you set it to '' instead, n is also seen as a line terminator but rn 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 'rn'. […] 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.

Answered By: Martijn Pieters

From Martijn Pieters the solution is:

with open(filename, "r", newline='rn') as f:

This answer was posted as an edit to the question disable the automatic change from rn to n in python by the OP lu1her under CC BY-SA 3.0.

Answered By: vvvvv