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

在 Python 中:

>>> "\N{BLACK SPADE SUIT}"
'♠'
>>> "\u2660"
'♠'

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

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

【问题讨论】:

  • 注意:在 python2 中,\N{} 转义序列仅适用于 unicode 字符串。

标签: python unicode python-3.x


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2017-12-12
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    相关资源
    最近更新 更多