如何在 Python 中找出 unicode 字符的编号/名称?

Posted

技术标签:

【中文标题】如何在 Python 中找出 unicode 字符的编号/名称?【英文标题】:How to find out number/name of unicode character in Python? 【发布时间】:2012-10-17 20:31:40 【问题描述】:

在 Python 中:

>>> "\NBLACK SPADE SUIT"
'♠'
>>> "\u2660"
'♠'

现在,假设我有一个我不知道名称或编号的角色。是否有一个 Python 函数可以提供这样的信息?

>>> wanted_function('♠')
["BLACK SPADE SUIT", "u2660"]

【问题讨论】:

注意:在 python2 中,\N 转义序列仅适用于 unicode 字符串。 【参考方案1】:

您可能会发现unicodedata 模块很方便:

>>> s = "\NBLACK SPADE SUIT"
>>> s
'♠'
>>> import unicodedata
>>> unicodedata.name(s)
'BLACK SPADE SUIT'
>>> ord(s)
9824
>>> hex(ord(s))
'0x2660'

【讨论】:

以上是关于如何在 Python 中找出 unicode 字符的编号/名称?的主要内容,如果未能解决你的问题,请参考以下文章

在.Net中找出Unicode字符名称

python unicode渲染:如何知道字体中是不是缺少unicode字符

如何在 Python 中动态指定 unicode 字符串?

如何在 Python 3 中获得组合 Unicode 字符的显示宽度?

如何在python中取消引用urlencoded unicode字符串?

如何在 Python 3 中迭代 Unicode 字符?