Python 用 <br /> 替换 \n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4369159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace \n with <br />
提问by Max Frai
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:
我正在用 Python 解析文件中的文本。我必须将所有换行符 (\n) 替换为
因为此文本将构建 html-content。例如,这是文件中的一些行:
'title\n'
Now I do:
现在我这样做:
thatLine.replace('\n', '<br />')
print thatLine
And I still see the text with newline after it.
而且我仍然看到后面带有换行符的文本。
采纳答案by Tim Pietzcker
Just for kicks, you could also do
只是为了踢,你也可以这样做
mytext = "<br />".join(mytext.split("\n"))
to replace all newlines in a string with <br />.
将字符串中的所有换行符替换为<br />.
回答by Falmarri
thatLine = thatLine.replace('\n', '<br />')
thatLine = thatLine.replace('\n', '<br />')
str.replace() returns a copy of the string, it doesn't modify the string you pass in.
str.replace() 返回字符串的副本,它不会修改您传入的字符串。
回答by erickb
thatLine = thatLine.replace('\n', '<br />')
Strings in Python are immutable. You might need to recreate it with the assignment operator.
Python 中的字符串是不可变的。您可能需要使用赋值运算符重新创建它。
回答by greggo
You could also have problems if the string has <, >or &chars in it, etc. Pass it to cgi.escape()to deal with those.
如果字符串中有<,>或&字符等,您也可能会遇到问题。将其传递cgi.escape()给处理这些问题。
http://docs.python.org/library/cgi.html?highlight=cgi#cgi.escape
http://docs.python.org/library/cgi.html?highlight=cgi#cgi.escape
回答by Mikko P?ri
For some reason using python3 I had to escape the "\"-sign
出于某种原因使用 python3 我不得不转义 "\"-sign
somestring.replace('\n', '')
Hope this helps someone else!
希望这对其他人有帮助!
回答by user3780389
To handle many newline delimiters, including character combinations like \r\n, use splitlines(see this related post) use the following:
要处理许多换行符,包括字符组合,例如\r\n,使用分割线(请参阅此相关帖子),请使用以下内容:
'<br />'.join(thatLine.splitlines())
回答by user2458922
The Problem is When you denote '\n'in the replace()call , '\n'is treated as a String length=4made out of ' \ n '
To get rid of this, use ascii notation. http://www.asciitable.com/
问题是当您'\n'在replace()调用中表示时,'\n'被视为String length=4由 做出来' \ n '
为了摆脱这一点,请使用 ascii 表示法。http://www.asciitable.com/
example:
例子:
newLine = chr(10)
thatLine=thatLine.replace(newLine , '<br />')
print(thatLine) #python3
print(thatLine) #python3
print thatLine #python2.
print thatLine #python2.
回答by DangerPaws
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.
我知道这是一个旧线程,但我尝试了建议的答案,不幸的是,简单地用换行符替换<br />并没有做我需要的。它只是将它们逐字呈现为文本。
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.
一种解决方案是像这样禁用自动转义:{% autoescape false %}{{ mystring }}{% endautoescape %}这可以正常工作,但如果您有用户提供的内容,则这不好。所以这对我来说也不是一个解决方案。
So this is what I used:
所以这就是我使用的:
In Python:
在 Python 中:
newvar = mystring.split('\n')
And then, passing newvarinto my Jinja template:
然后,传入newvar我的 Jinja 模板:
{% for line in newvar %}
<br />{{ line }}
{% endfor %}

