wbe*_*rry 2 python windows unicode utf-16
Ubuntu 11.10:
$ python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u'\U0001f44d'
>>> len(x)
1
>>> ord(x[0])
128077
Run Code Online (Sandbox Code Playgroud)
Windows 7的:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u'\U0001f44d'
>>> len(x)
2
>>> ord(x[0])
55357
Run Code Online (Sandbox Code Playgroud)
我的Ubuntu体验是使用发行版中的默认解释器.对于Windows 7,我下载并安装了从python.org链接的推荐版本.我自己没有编译其中任何一个.
差异的本质对我来说很清楚.(在Ubuntu上,字符串是一系列代码点;在Windows 7上是一系列UTF-16代码单元.)我的问题是:
unicode在Windows上手动计算字符串中的代理项目(blech)之外,是否有任何解决此问题的方法?dan*_*n04 11
在Ubuntu上,你有一个"宽"的Python构建,其中字符串是UTF-32/UCS-4.不幸的是,这还不适用于Windows.
基于对宽字符的请求很少这一事实,Windows构建将会缩短一段时间,这些请求主要来自能够购买自己的Python的硬核程序员,而Windows本身则强烈偏向于16位字符.
Python 3.3将具有灵活的字符串表示,您无需关心Unicode字符串是使用16位还是32位代码单元.
在此之前,您可以从UTF-16字符串中获取代码点
def code_points(text):
utf32 = text.encode('UTF-32LE')
return struct.unpack('<{}I'.format(len(utf32) // 4), utf32)
Run Code Online (Sandbox Code Playgroud)