将数字字符引用表示法转换为unicode字符串

时间:2013-06-10 07:21:12

标签: python unicode encoding

是否有标准的,最好是Pythonic的方式将&#xxxx;表示法转换为正确的unicode字符串?

例如,

מפגשי

应转换为:

מפגשי

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

1 个答案:

答案 0 :(得分:9)

使用HTMLParser.HTMLParser()

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

它也是standard library的一部分。


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

>>> from html.parser import HTMLParser
>>> h = HTMLParser()
>>> s = 'מפגשי'
>>> print(h.unescape(s))
מפגשי
相关问题