Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable the automatic change from \r\n to \n in python

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 \r\n) knowing that the "lines" contain some '\n' that I want to keep.

My problem is that Python transforms the file's "\r\n" 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?

EDIT: Solution:

with open(filename, "r", newline='\r\n') as f:
like image 565
lu1her Avatar asked Apr 27 '16 13:04

lu1her


People also ask

What does the New \n character mean in Python?

The new line character in Python is \n. It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character>, which <character> is the character that will be used to separate the lines. I really hope that you liked my article and found it helpful.

How do I remove N and T from a string in Python?

Remove n From String Using str.replace () Method in Python The other way to remove n and t from a string is to use the str.replace () method. We should keep in mind that the str.replace () method will replace the given string from the whole thing, not just from the string’s start or end.

How to use carriage return in Python with a newline character?

Using carriage return in Python with a newline character In this example, we will be using ‘’ with the new line character ( ) in the string program. Firstly, we have taken an input string as a string. Then, we have applied and in multiple places of the string. is for newline.

How to remove [&N&] from string using regex method in Python?

Remove [&n&] From String Using regex Method in Python To remove [&n&] from the string, we can use the re.sub () method. The below code example demonstrates how to remove [&n&] using the re.sub () method. n is the new line’s regular express pattern, and it will be replaced with the empty string - "". import re string = "Hello, nhow are youn?"


1 Answers

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.

like image 73
Martijn Pieters Avatar answered Oct 14 '22 03:10

Martijn Pieters