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:
'titlen'
Now I do:
thatLine.replace('n', '<br />')
print thatLine
And I still see the text with newline after it.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
thatLine = thatLine.replace('n', '<br />')
str.replace() returns a copy of the string, it doesn’t modify the string you pass in.
Method 2
Just for kicks, you could also do
mytext = "<br />".join(mytext.split("n"))
to replace all newlines in a string with <br />.
Method 3
For some reason using python3 I had to escape the “”-sign
somestring.replace('\n', '')
Hope this helps someone else!
Method 4
To handle many newline delimiters, including character combinations like rn, use splitlines (see this related post) use the following:
'<br />'.join(thatLine.splitlines())
Method 5
thatLine = thatLine.replace('n', '<br />')
Strings in Python are immutable.
You might need to recreate it with the assignment operator.
Method 6
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
Method 7
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 .
Method 8
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 %}
Method 9
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.
Method 10
If you are wanting to do this using replace, and the
is not working for you (as was the case for me), you can use the following:
text_to_split = r'Hellonn I am a test.'
split_text = text_to_split.replace('\n', 'n')
You first escape the in the sample text, and then by not escaping in the replacement text, it will generate a new line.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0