【问题标题】:Convert numeric character reference notation to unicode string将数字字符引用表示法转换为 unicode 字符串
【发布时间】:2013-06-05 19:17:04
【问题描述】:

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

例如,

מפגשי

应转换为:

מפגשי

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

【问题讨论】:

标签: python unicode encoding


【解决方案1】:

使用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))
מפגשי

【讨论】:

  • unescape 似乎是内部的且未记录。有没有“官方”的方式?
  • @thg435 我不知道,抱歉
  • 我也没找到。好吧,这有点糟糕,不是吗?
  • 自 Python 3.4 以来似乎有一种官方方式使用html.unescape(s)
猜你喜欢
  • 2019-05-20
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 2015-03-29
相关资源
最近更新 更多