When I print the string (in Python) coming from a website I scraped it from, it looks like this:
"His this
is
a sample
String"
It does not show the \n
breaks. this is what I see in a Python interpreter.
And I want to convert it to HTML that will add in the line breaks. I was looking around and didn't see any libraries that do this out of the box.
I was thinking BeautifulSoup, but wasn't quite sure.
如果您有从文件中读取的字符串,则可以通过执行以下操作将\\n
替换为<br>
,这是html中的换行符:
my_string.replace('\n', '<br>')
I believe this will work
for line in text:
for char in line:
if char == "/n":
text.replace(char, "<br>")
You can use the python replace(...)
method to replace all line breaks with the html version <br>
and possibly surround the string in a paragraph tag <p>...</p>
. Let's say the name of the variable with the text is text
:
html = "<p>" + text.replace("\n", "<br>") + "</p>"
searching for this answer in found this, witch is likely better because it encodes all characters, at least for python 3 Python – Convert HTML Characters To Strings
# import html
import html
# Create Text
text = 'Γeeks for Γeeks'
# It Converts given text To String
print(html.unescape(text))
# It Converts given text to HTML Entities
print(html.escape(text))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.