繁体   English   中英

使用 Python 获取字符的 unicode 代码点

[英]Get unicode code point of a character using Python

在 Python API 中,有没有办法提取单个字符的 unicode 代码点?

编辑:以防万一,我使用的是 Python 2.7。

如果我正确理解你的问题,你可以这样做。

>>> s='㈲'
>>> s.encode("unicode_escape")
b'\\u3232'

将 unicode 转义码显示为源字符串。

>>> ord(u"ć")
263
>>> u"café"[2]
u'f'
>>> u"café"[3]
u'\xe9'
>>> for c in u"café":
...     print repr(c), ord(c)
... 
u'c' 99
u'a' 97
u'f' 102
u'\xe9' 233

通常,您只需执行ord(character)即可找到ord(character)的代码点。 不过,为了完整性,Unicode Supplementary Multilingual Plane 中的宽字符在窄 Python 构建中表示为代理对(即两个代码单元),因此在这种情况下,我经常需要做这个小工作:

def get_wide_ordinal(char):
    if len(char) != 2:
        return ord(char)
    return 0x10000 + (ord(char[0]) - 0xD800) * 0x400 + (ord(char[1]) - 0xDC00)

不过,这在大多数应用程序中很少见,所以通常只使用ord()

事实证明,正确地做到这一点相当棘手:Python 2 和 Python 3 在从字符串中提取 Unicode 代码点时存在一些微妙的问题。

在 Python 3.3 之前,可以使用以下两种模式之一编译 Python:

  1. sys.maxunicode == 0x10FFFF

在这种模式下,Python 的 Unicode 字符串支持从 U+0000 到 U+10FFFF 的全范围 Unicode 代码点。 一个代码点由一个字符串元素表示:

>>> import sys
>>> hex(sys.maxunicode)
'0x10ffff'
>>> len(u'\U0001F40D')
1
>>> [c for c in u'\U0001F40D']
[u'\U0001f40d']

这是 Linux 上 Python 2.7 的默认设置,以及所有操作系统中 Python 3.3 及更高版本的普遍设置。

  1. sys.maxunicode == 0xFFFF

在这种模式下,Python 的 Unicode 字符串仅支持从 U+0000 到 U+FFFF 的 Unicode 码位范围。 从 U+10000 到 U+10FFFF 的任何代码点都使用 UTF-16 编码中的一对字符串元素表示:

>>> import sys
>>> hex(sys.maxunicode)
'0xffff'
>>> len(u'\U0001F40D')
2
>>> [c for c in u'\U0001F40D']
[u'\ud83d', u'\udc0d']

这是 macOS 和 Windows 上 Python 2.7 的默认设置。

这种运行时差异使得编写 Python 模块来操作 Unicode 字符串作为一系列代码点非常不方便。

代码点模块

为了解决这个问题,我为PyPI贡献了一个新的模块codepoints

https://pypi.python.org/pypi/codepoints/1.0

该模块通过公开 API 来解决这个问题,无论sys.maxunicode ::

>>> hex(sys.maxunicode)
'0xffff'
>>> snake = tuple(codepoints.from_unicode(u'\U0001F40D'))
>>> len(snake)
1
>>> snake[0]
128013
>> hex(snake[0])
'0x1f40d'
>>> codepoints.to_unicode(snake)
u'\U0001f40d'

蟒蛇2

>>> print hex(ord(u'人'))
0x4eba

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM