今天写wxPython时,发现一个问题:
UnicodeDecodeError: ‘gbk’ codec can’t decode bytes in position 2-3: illegal multibyte sequence
但我在代码头有注释
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
这是为什么呢.我把UTF-8改为GBK后就一切正常了,后来问了下limodou,原来是unicode的问题,我自己安装的wxPython是unicode版本的,不是ansi的,所以,在里面所有字符都是以unicode对象存在的,而ansi的就以string对象存在,所以对于“中国”这种中文字符显然就要用u”中国”来代替
这是wxPython官方的话
The Win32 version of wxPython is distributed as a set of standard self-installing executables. This allows you the convenience of using the Start Menu and Add/Remove Programs just like any other Windows software.
There are two versions of wxPython for each of the supported Python versions on Win32. They are nearly identical, except one of them has been compiled with support for the Unicode version of the platform APIs. Unless you’ve been told differently, you probably want to get the Unicode build of wxPython. Although they are not nativly Unicode like NT-based systems, the Unicode build of wxPython will also mostly work on Windows 98/Me systems using a Microsoft hack called MSLU (or unicows.dll) that translates unicode API calls to ansi API calls. However the coverage of the API is not complete so there are some difficult bugs lurking in there, so the best thing to do is to upgrade your machines if possible, otherwise Windows 98/Me users may want to try the ANSI build instead.