![](/img/trans.png)
[英]Convert a symbol to its 4 digit unicode escape representation and vice versa
[英]Convert HTML entities to Unicode and vice versa
如何在 Python 中將 HTML 實體轉換為 Unicode 和反之亦然?
至於“反之亦然”(我需要自己,導致我找到這個沒有幫助的問題,然后是另一個有答案的網站):
u'some string'.encode('ascii', 'xmlcharrefreplace')
將返回一個純字符串,其中任何非 ascii 字符都轉換為 XML (HTML) 實體。
你需要有BeautifulSoup 。
from BeautifulSoup import BeautifulStoneSoup
import cgi
def HTMLEntitiesToUnicode(text):
"""Converts HTML entities to unicode. For example '&' becomes '&'."""
text = unicode(BeautifulStoneSoup(text, convertEntities=BeautifulStoneSoup.ALL_ENTITIES))
return text
def unicodeToHTMLEntities(text):
"""Converts unicode to HTML entities. For example '&' becomes '&'."""
text = cgi.escape(text).encode('ascii', 'xmlcharrefreplace')
return text
text = "&, ®, <, >, ¢, £, ¥, €, §, ©"
uni = HTMLEntitiesToUnicode(text)
htmlent = unicodeToHTMLEntities(uni)
print uni
print htmlent
# &, ®, <, >, ¢, £, ¥, €, §, ©
# &, ®, <, >, ¢, £, ¥, €, §, ©
Python 2.7 和 BeautifulSoup4 的更新
Unescape -- Unicode HTML 到 unicode 與htmlparser
(Python 2.7 標准庫):
>>> escaped = u'Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood'
>>> from HTMLParser import HTMLParser
>>> htmlparser = HTMLParser()
>>> unescaped = htmlparser.unescape(escaped)
>>> unescaped
u'Monsieur le Cur\xe9 of the \xabNotre-Dame-de-Gr\xe2ce\xbb neighborhood'
>>> print unescaped
Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood
Unescape -- Unicode HTML 到 unicode 與bs4
(BeautifulSoup4):
>>> html = '''<p>Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood</p>'''
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> soup.text
u'Monsieur le Cur\xe9 of the \xabNotre-Dame-de-Gr\xe2ce\xbb neighborhood'
>>> print soup.text
Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood
轉義 -- Unicode 到 unicode HTML 與bs4
(BeautifulSoup4):
>>> unescaped = u'Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood'
>>> from bs4.dammit import EntitySubstitution
>>> escaper = EntitySubstitution()
>>> escaped = escaper.substitute_html(unescaped)
>>> escaped
u'Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood'
正如hekevintran回答所暗示的,您可以使用cgi.escape(s)
對字符串進行編碼,但請注意,在 function 中,quote 的編碼默認為 false,並且將quote=True
關鍵字參數與您的字符串一起傳遞可能是個好主意。 但即使通過quote=True
, function 也不會轉義單引號( "'"
)(由於這些問題, function 自 3.2 版以來已被棄用)
建議使用html.escape(s)
而不是cgi.escape(s)
。 (版本 3.2 中的新功能)
html.unescape(s)
已在 3.4 版本中引入。
因此,在 python 3.4 中,您可以:
html.escape(text).encode('ascii', 'xmlcharrefreplace').decode()
將特殊字符轉換為 HTML 實體。html.unescape(text)
用於將 HTML 實體轉換回純文本表示形式。$ python3 -c "
> import html
> print(
> html.unescape('&©—')
> )"
&©—
$ python3 -c "
> import html
> print(
> html.escape('&©—')
> )"
&©—
$ python2 -c "
> from HTMLParser import HTMLParser
> print(
> HTMLParser().unescape('&©—')
> )"
&©—
$ python2 -c "
> import cgi
> print(
> cgi.escape('&©—')
> )"
&©—
HTML 只嚴格要求&
(與號)和<
(左尖括號/小於號)被轉義。 https://html.spec.whatwg.org/multipage/parsing.html#data-state
對於python3
使用html.unescape()
:
import html
s = "&"
decoded = html.unescape(s)
# &
如果像我這樣的人想知道為什么有些實體編號(代碼)像™ (for trademark symbol), € (for euro symbol)
™ (for trademark symbol), € (for euro symbol)
™ (for trademark symbol), € (for euro symbol)
未正確編碼,原因是在 ISO-8859-1(又名 Windows-1252)中未定義這些字符。
另請注意,從 html5 開始的默認字符集是 utf-8 它是 html4 的 ISO-8859-1
所以,我們將不得不以某種方式解決(首先找到並替換那些)
Mozilla 文檔中的參考(起點)
https://developer.mozilla.org/en-US/docs/Web/Guide/Localizations_and_character_encodings
我使用以下 function 將從 xls 文件中提取的 unicode 轉換為 html 文件,同時保留 xls 文件中的特殊字符:
def html_wr(f, dat):
''' write dat to file f as html
. file is assumed to be opened in binary format
. if dat is nul it is replaced with non breakable space
. non-ascii characters are translated to xml
'''
if not dat:
dat = ' '
try:
f.write(dat.encode('ascii'))
except:
f.write(html.escape(dat).encode('ascii', 'xmlcharrefreplace'))
希望這對某人有用
#!/usr/bin/env python3
import fileinput
import html
for line in fileinput.input():
print(html.unescape(line.rstrip('\n')))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.