23

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.

Jack Slingerland
  • 2,651
  • 5
  • 34
  • 56

7 Answers7

34

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

"\n".join(mytext.splitlines())
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    This should be the solution – geotheory Dec 19 '15 at 13:29
  • I would appreciate if you would explain why you recommend `splitlines` over regex and other approaches.... – Steve Jun 04 '19 at 17:24
  • mostly future proofing ... imagine some os that comes out in 2 years that uses `\r\r\n\t` as its newline ... pythons builtin module will still splitlines as expected (assuming there is a version of python for this OS) – Joran Beasley Jun 05 '19 at 16:04
  • 4
    This will not replace \r\n when it is at the end of the string. It will just remove. – paul Sep 28 '19 at 14:21
  • 1
    It insert 2 `\n` not one cause `splitlines()` leaves and empty string in the array of splits. – Andrea Franchini Dec 02 '19 at 14:33
16
mytext.replace(r"\r\n", r"\n")

The 'r' denotes a raw string, which tells python to interpret the backslashes in the text as literal characters and not as escape characters.

Lanaru
  • 9,421
  • 7
  • 38
  • 64
5

"\n".join(mytext.splitlines()) This works for me. mytext.replace(r"\r\n", r"\n"), this not work.

Lensys
  • 73
  • 1
  • 11
micheal-xu
  • 51
  • 1
  • 2
3

This is a solution to try if any of the above did not work (which was the case for me using the Anaconda Distribution of Python3).

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

This has to do with \ being used as an escape character. I thought that the above answers that used the raw string formatter would achieve the same thing, but for whatever reason that did not work for me, and this did.

0

Sorry, I misread your question:

In that case, you should prefix your string with a r to use raw strings:

mytext.replace(r"\r\n", r"\n")
Gerard Yin
  • 1,283
  • 1
  • 12
  • 24
0

python will auto convert '\r\n' to '\n' when you read file to variable, and vice versa. But if you write back to file with "binary mode", then python will write exactly your content to file. so simply read file to variable and write back with binary mode will auto convert '\r\n' to '\n' in windows platform.

file_name = 'test.sh'
file_content = ''
with open(file_name) as f:
    file_content = f.read()

with open(file_name, 'wb') as f:
    f.write(file_content)
Luke Cheung
  • 479
  • 6
  • 8
0

LINEBREAK = "\n"

with open(filename) as f:
    s = f.read()
    s = LINEBREAK.join(s.splitlines())
with open(new_filename, "wb") as f:
    f.write(s + "\n")

If you want to use '\r\n' as linebreak, just change the LINEBREAK.