8

是否有一种标准的,最好是 Pythonic 的方法来将&#xxxx;符号转换为正确的 unicode 字符串?

例如,

מפגשי

应转换为:

מפגשי

使用字符串操作可以很容易地完成它,但我想知道是否有一个标准库。

4

1 回答 1

11

使用HTMLParser.HTMLParser()

>>> from HTMLParser import HTMLParser
>>> h = HTMLParser()
>>> s = "מפגשי"
>>> print h.unescape(s)
מפגשי

它也是标准库的一部分。


但是,如果您使用的是 Python 3,则必须从以下位置导入html.parser

>>> from html.parser import HTMLParser
>>> h = HTMLParser()
>>> s = 'מפגשי'
>>> print(h.unescape(s))
מפגשי
于 2013-06-10T07:32:07.657 回答