使用Python获取字符的unicode代码点
在Python API中,有没有办法提取单个字符的unicode代码点?
编辑:以防万一,我使用的是 Python 2.7。
In Python API, is there a way to extract the unicode code point of a single character?
Edit: In case it matters, I'm using Python 2.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我正确理解你的问题,你可以这样做。
将 unicode 转义码显示为源字符串。
If I understand your question correctly, you can do this.
Shows the unicode escape code as a source string.
事实证明,要做到这一点相当棘手:Python 2 和 Python 3 在从字符串中提取 Unicode 代码点时存在一些微妙的问题。
在 Python 3.3 之前,可以使用以下两种模式之一来编译 Python:
sys.maxunicode == 0x10FFFF
在这种模式下,Python 的 Unicode 字符串支持从 U+0000 到U+10FFFF。一个代码点由一个字符串元素表示:
这是 Linux 上 Python 2.7 的默认设置,也是 Python 3.3 及更高版本所有操作系统上的默认设置。
sys.maxunicode == 0xFFFF
在此模式下,Python 的 Unicode 字符串仅支持从 U+0000 到 U+FFFF 的 Unicode 代码点范围。从 U+10000 到 U+10FFFF 的任何代码点都使用 UTF-16 编码中的一对字符串元素表示::
这是 macOS 和 Windows 上的 Python 2.7 的默认设置。
这种运行时差异使得编写 Python 模块来将 Unicode 字符串作为一系列代码点进行操作非常不方便。
<
为了解决这个问题,我向
PyPI
贡献了一个新模块codepoints
:a href="https://pypi.python.org/pypi/codepoints/1.0 " rel="noreferrer">https://pypi.python.org/pypi/codepoints/1.0
该模块通过公开 API 将 Unicode 字符串与代码点列表相互转换来解决该问题,而不管底层是什么环境对于
sys.maxunicode
::Turns out getting this right is fairly tricky: Python 2 and Python 3 have some subtle issues with extracting Unicode code points from a string.
Up until Python 3.3, it was possible to compile Python in one of two modes:
sys.maxunicode == 0x10FFFF
In this mode, Python's Unicode strings support the full range of Unicode code points from U+0000 to U+10FFFF. One code point is represented by one string element:
This is the default for Python 2.7 on Linux, as well as universally on Python 3.3 and later across all operating systems.
sys.maxunicode == 0xFFFF
In this mode, Python's Unicode strings only support the range of Unicode code points from U+0000 to U+FFFF. Any code points from U+10000 through U+10FFFF are represented using a pair of string elements in the UTF-16 encoding::
This is the default for Python 2.7 on macOS and Windows.
This runtime difference makes writing Python modules to manipulate Unicode strings as series of codepoints quite inconvenient.
The codepoints module
To solve this, I contributed a new module
codepoints
toPyPI
:https://pypi.python.org/pypi/codepoints/1.0
This module solves the problem by exposing APIs to convert Unicode strings to and from lists of code points, regardless of the underlying setting for
sys.maxunicode
::通常,您只需执行
ord(character)
即可查找字符的代码点。不过,为了完整起见,Unicode 补充多语言平面中的宽字符在狭窄的 Python 构建中表示为代理对(即两个代码单元),因此在这种情况下,我经常需要做这个小解决方法:尽管这在大多数应用程序中很少见,所以通常只需使用
ord()
即可。Usually, you just do
ord(character)
to find the code point of a character. For completeness though, wide characters in the Unicode Supplementary Multilingual Plane are represented as surrogate pairs (i.e. two code units) in narrow Python builds, so in that case I often needed to do this small work-around:This is rare in most applications though, so normally just use
ord()
.蟒蛇2
python2