如何在 Python 中找出 unicode 字符的编号/名称? [英] How to find out number/name of unicode character in Python?

查看:39
本文介绍了如何在 Python 中找出 unicode 字符的编号/名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中:

<预><代码>>>>"\N{BLACK SPADE SUIT}"'♠'>>>\u2660"'♠'

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

<预><代码>>>>want_function('♠')[黑桃套装"、u2660"]

解决方案

您可以找到 unicodedata模块方便:

<预><代码>>>>s = "\N{黑桃套装}">>>秒'♠'>>>导入 unicodedata>>>unicodedata.name(s)'黑桃套装'>>>订单9824>>>十六进制(ord(s))'0x2660'

In Python:

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

Now, let's say I have a character which I don't know the name or number for. Is there a Python function which gives this information like this?

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

解决方案

You may find the unicodedata module handy:

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

这篇关于如何在 Python 中找出 unicode 字符的编号/名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆