55

I'm parsing text from file with Python. I have to replace all newlines (\n) with
cause this text will build html-content. For example, here is some line from file:

'title\n'

Now I do:

thatLine.replace('\n', '<br />')
print thatLine

And I still see the text with newline after it.

Max Frai
  • 57,874
  • 75
  • 193
  • 301
  • 4
    Consider a CSS only solution: http://stackoverflow.com/a/7602751/74449 to reduce potential for XSS – Myster Mar 19 '13 at 22:31

9 Answers9

183

thatLine = thatLine.replace('\n', '<br />')

str.replace() returns a copy of the string, it doesn't modify the string you pass in.

Falmarri
  • 46,415
  • 39
  • 146
  • 189
67

Just for kicks, you could also do

mytext = "<br />".join(mytext.split("\n"))

to replace all newlines in a string with <br />.

Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
  • 25
    Thanks for accepting this, but please accept @Falmarri's answer instead. It's much more Pythonic. This here is more of a funny, backwards way of doing it. Not really recommended... – Tim Pietzcker Dec 06 '10 at 17:50
  • 1
    Don't know about @Falmarri's answer being "more Pythonic", but it's more efficient and probably faster. – martineau Dec 06 '10 at 20:28
  • 4
    @martineau: I meant this in the sense of "There should be one -- and preferably only one -- obvious way to do it.". And I'm pretty sure that mine isn't the one. – Tim Pietzcker Dec 06 '10 at 20:54
23

For some reason using python3 I had to escape the "\"-sign

somestring.replace('\\n', '')

Hope this helps someone else!

Mikko P
  • 461
  • 6
  • 7
10

To handle many newline delimiters, including character combinations like \r\n, use splitlines (see this related post) use the following:

'<br />'.join(thatLine.splitlines())
Community
  • 1
  • 1
teichert
  • 3,123
  • 1
  • 23
  • 34
8
thatLine = thatLine.replace('\n', '<br />')

Strings in Python are immutable. You might need to recreate it with the assignment operator.

Stefan van den Akker
  • 6,242
  • 7
  • 43
  • 62
erickb
  • 5,925
  • 4
  • 24
  • 19
4

You could also have problems if the string has <, > or & chars in it, etc. Pass it to cgi.escape() to deal with those.

http://docs.python.org/library/cgi.html?highlight=cgi#cgi.escape

Stefan van den Akker
  • 6,242
  • 7
  • 43
  • 62
greggo
  • 2,761
  • 2
  • 21
  • 19
3

The Problem is When you denote '\n' in the replace() call , '\n' is treated as a String length=4 made out of ' \ n '
To get rid of this, use ascii notation. http://www.asciitable.com/

example:

newLine = chr(10)
thatLine=thatLine.replace(newLine , '<br />')

print(thatLine) #python3

print thatLine #python2 .

user2458922
  • 1,456
  • 1
  • 15
  • 27
1

I know this is an old thread but I tried the suggested answers and unfortunately simply replacing line breaks with <br /> didn't do what I needed it to. It simply rendered them literally as text.

One solution would have been to disable autoescape like this: {% autoescape false %}{{ mystring }}{% endautoescape %} and this works fine but this is no good if you have user-provided content. So this was also not a solution for me.

So this is what I used:

In Python:

newvar = mystring.split('\n')

And then, passing newvar into my Jinja template:

{% for line in newvar %}
    <br />{{ line }}
{% endfor %}
DangerPaws
  • 555
  • 1
  • 5
  • 18
0

If in case you are trying to replace the /n of a string to </br>, i am speculating that you want to parse line break in HTML view.

word-break: break-all,
white-space: pre-wrap;

Adding these to styles to parent div/container of the text will solve your problem without any string manipulations.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
JJY9
  • 35
  • 8