如何在Python中找到unicode字符的数量/名称?

Pio*_*icz 34 python unicode python-3.x

在Python中:

>>>"\N{BLACK SPADE SUIT}"
>>>'?'
>>>"\u2660"
>>>'?'
Run Code Online (Sandbox Code Playgroud)

现在,假设我有一个我不知道名字或号码的角色.是否有Python函数提供如下信息:

>>>wanted_function('?')
>>>["BLACK SPADE SUIT", "u2660"]
Run Code Online (Sandbox Code Playgroud)

DSM*_*DSM 52

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

>>> s = "\N{BLACK SPADE SUIT}"
>>> s
'?'
>>> import unicodedata
>>> unicodedata.name(s)
'BLACK SPADE SUIT'
>>> ord(s)
9824
>>> hex(ord(s))
'0x2660'
Run Code Online (Sandbox Code Playgroud)