如何用正确的 unicode 替换 unicode 字符串中的 HTML 实体?
u'"HAUS Kleider" - Über das Bekleiden und Entkleiden, das VerhŸllen und Veredeln'
至
u'"HAUS-Kleider" - Über das Bekleiden und Entkleiden, das Verhüllen und Veredeln'
编辑
事实上,实体是错误的。看起来 BeautifulSoup 已经满足了它。
所以问题是:如何处理utf-8编码的String和BeautifulSoup?
from BeautifulSoup import BeautifulSoup
f = open('path_to_file','r')
lines = [i for i in f.readlines()]
soup = BeautifulSoup(''.join(lines))
allArticles = []
for row in rows:
l =[]
for r in row.findAll('td'):
l += [r.string] # here things seem to go wrong
allArticles+=[l]
Ü -> Ÿ
而不是 Ü
但实际上我不想更改编码。
>>> soup.originalEncoding
'utf-8'
但我无法生成正确的 unicode 字符串
最佳答案
我想你需要的是ICU transliterators 。我认为有一种方法可以将 HTML 实体音译为 Unicode。
尝试使用您想要的音译器ID Hex/XML-Any
。在演示页面上,您可以选择“插入示例:化合物”,然后在“化合物1”框中输入Hex/XML-Any
,在框中添加一些输入数据,然后按“转换”。是this有帮助吗?
有一个 Python ICU 绑定(bind),但我认为它没有得到很好的处理。
关于python - 如何处理utf-8编码的String和BeautifulSoup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054551/