Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing "\r\n" with "\n"

Tags:

I have some text data that is printing out the actual characters "\r\n" (so four characters total). I'd like to replace those four characters with the single "\n" character, but I can't seem to make Python do it for me. I'm currently trying:

mytext.replace("\r\n", "\n")

But that just prints out "\n" (two characters, not one). I feel like I'm probably missing something obvious, but any help would be appreciated.

like image 561
Jack Slingerland Avatar asked Aug 16 '12 14:08

Jack Slingerland


People also ask

How do you convert N to a new line?

If your items are separated by pipes only, type the pipe character (“|”). Next, locate the “Search Mode” box and select “Extended.” This allows the replace action to convert “\n” into a newline character. Then, click “Replace All” on the right side of the window.

How do you convert N to a new line in Python?

Create a string containing line breaks Inserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code.

How do I search for a new line in Notepad?

In Notepad++ press Ctr+H to open the “Find and Replace” window. Under Search Mode: choose “Regular expression” and then check the “matches newline” checkbox.


1 Answers

I would recommend using splitlines instead of a regex or search/replace

"\n".join(mytext.splitlines())
like image 133
Joran Beasley Avatar answered Nov 11 '22 21:11

Joran Beasley