Replace '\n' in a string in Python 2.7
Asked Answered
F

4

6

This is my file.txt:

Egg and Bacon;
Egg, sausage and Bacon
Egg and Spam;
Spam Egg Sausage and Spam;
Egg, Bacon and Spam;

I wanna convert the newLine '\n' to ' $ '. I just used:

f = open(fileName)
text = f.read()      
text = text.replace('\n',' $ ')
print(text)

This is my output:

$ Spam Egg Sausage and Spam;

and my output must be like:

Egg and Bacon; $ Egg, sausage and Bacon $ Egg ...

What am I doing wrong? I'm using #-*- encoding: utf-8 -*-

Thank you.

Flagstone answered 25/3, 2015 at 9:4 Comment(4)
Cannot reproduce; working as expected for me.Cadel
Does your text contain '\r' characters? What does print repr(text) print? I had no problems running your code; it works for me as intended.Westphalia
print(repr(text)) = 'Egg and Bacon;\r\nEgg, sausage and Bacon\r\nEgg and Spam;\r\nSpam Egg Sausage and Spam;\r\n' I made this change "replace('\r\n', ' $ ')" and now it works fine! Thank you!!Flagstone
Does this answer your question? Is universal newlines mode supposed to be default behaviour for open() in Python 2.7?Chardin
A
15

It is possible that your newlines are represented as \r\n. In order to replace them you should do:

text.replace('\r\n', ' $ ')

For a portable solution that works on both UNIX-like systems (which uses \n) and Windows (which uses \r\n), you can substitute the text using a regex:

>>> import re
>>> re.sub('\r?\n', ' $ ', 'a\r\nb\r\nc')
'a $ b $ c'
>>> re.sub('\r?\n', ' $ ', 'a\nb\nc')
'a $ b $ c'
Arron answered 25/3, 2015 at 9:14 Comment(2)
An additional comment: I would recommend doing first text = text.replace('\r\n','\n') and then doing text = text.replace('\n', ' $ '). This works for all files, for the ones having '\n' line separators and for the ones having '\r\n' line separators.Westphalia
Yes, I agree that your regex solution is better. Got already my upvote, though.Westphalia
M
6

You can use splitlines.

lines = """Egg and Bacon;
Egg, sausage and Bacon
Egg and Spam;
Spam Egg Sausage and Spam;
Egg, Bacon and Spam;"""

print(" $ ".join(lines.splitlines()))
Egg and Bacon; $ Egg, sausage and Bacon $ Egg and Spam; $ Spam Egg Sausage and Spam; $ Egg, Bacon and Spam;

Or simply use rstrip and join on the file object without reading all into memory:

with open("in.txt") as f: 
    print(" $ ".join(line.rstrip() for line in f))
    Egg and Bacon; $ Egg, sausage and Bacon $ Egg and Spam; $ Spam Egg Sausage and Spam; $ Egg, Bacon and Spam;

Which is a much more efficient solution than reading all the file into memory and using a regex. You should also always use with to open your files as it closes them automatically.

rstrip will remove \n \r\n etc..

In [41]: s = "foo\r\n"
In [42]: s.rstrip()
Out[42]: 'foo'    
In [43]: s = "foo\n"    
In [44]: s.rstrip()
Out[44]: 'foo'
Monogamous answered 25/3, 2015 at 9:48 Comment(0)
M
6
text = text.replace('\\n', '')
Masseuse answered 9/10, 2020 at 15:19 Comment(1)
This is wrong; the input does not actually contain backslashes.Chardin
W
0

import re content = "great\n\rwork" re.sub('(\r|\n)+', ' ',content)

great work

Wangle answered 25/7, 2024 at 9:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.