如何在python中将unicode字符转义为符号实体名称?

如何在python中将unicode字符转义为符号实体名称?,python,escaping,Python,Escaping,我想要达到的是 Í -> í ø -> ø ñ -> ñ ... 在python中有没有一种标准的方法来实现这一点,或者我必须创建自己的字典并使用它来手动搜索字符 我在这里找到了很多其他方法的提示,但没有一个能回答我的问题。您正在寻找: 试试这个: import htmlentitydefs def EscapeUnicode(character): return "&%s;" % htmle

我想要达到的是

Í -> í
ø -> ø
ñ -> ñ
...
在python中有没有一种标准的方法来实现这一点,或者我必须创建自己的字典并使用它来手动搜索字符

我在这里找到了很多其他方法的提示,但没有一个能回答我的问题。

您正在寻找:

试试这个:

import htmlentitydefs

def EscapeUnicode(character):
    return "&%s;" % htmlentitydefs.codepoint2name[ord(character)]
我想你是说?
import htmlentitydefs

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