【问题标题】:How to escape unicode characters to symbol entitiy names in python?python - 如何将unicode字符转义为python中的符号实体名称?
【发布时间】:2011-08-13 16:33:20
【问题描述】:

我想要达到的是

Í -> í
ø -> ø
ñ -> ñ
...

在 python 中是否有标准的方法,或者我必须创建自己的字典并使用它来手动转义字符?

我在 SO 上找到了很多关于其他方式的提示,但没有一个能回答我的问题。

【问题讨论】:

标签: python escaping


【解决方案1】:

你正在寻找htmlentitydefs:

In [217]: import htmlentitydefs

In [224]: ['&'+htmlentitydefs.codepoint2name[ord(x)]+';' for x in u'Íøñ']
Out[224]: ['Í', 'ø', 'ñ']

【讨论】:

    【解决方案2】:

    试试这个:

    import htmlentitydefs
    
    def EscapeUnicode(character):
        return "&%s;" % htmlentitydefs.codepoint2name[ord(character)]
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 2015-03-07
      • 1970-01-01
      • 2016-01-05
      • 2012-10-17
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多