周海汉 /文
2010.1.6
python版本,python 2.6+,wxpython 2.8+
遇到wxpython 如何在windows下同时显示中文日文韩文越南拼音的问题。
windows下python如何显示utf8编码呢?
数据文件和python源文件都是utf8。遇到中文能显示,但韩语显示为问号的问题。并不是读文件编码有问题,因为linux下并无问题。
cmd命令行下
>>> u=unichr(0xc911)
>>> print u
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character u'/uc911' in position 0:
illegal multibyte sequence
>>> print u.encode('gbk')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character u'/uc911' in position 0:
illegal multibyte sequence
>>> print u.encode('mbcs')
?
显示为问号。
u必须经过encode才能打印,所以对CJK支持非常有限。因为gbk不能显示韩语等。同时命令行字体也对显示各国语言有限制。
>>> u='일'
Unsupported characters in input
>>> u=u'일' Unsupported characters in input >>> u=u'일'.decode('gbk') Unsupported characters in input >>> u=u'일'.decode('utf8') Unsupported characters in input >>> u='일'.decode('gbk') Unsupported characters in input >>> u='일'.decode('utf8') Unsupported characters in input >>> u=unichr(0xc911) >>> print u >>> print u.encode('utf8') Traceback (most recent call last): File "<pyshell#41>", line 1, in <module> print u.encode('utf8') File "C:/Python26/lib/site-packages/wx-2.8-msw-unicode/wx/_core.py", line 7845, in write self.__write(text) File "C:/Python26/lib/site-packages/wx-2.8-msw-unicode/wx/_core.py", line 7850, in __write self.text.AppendText(text) File "C:/Python26/lib/site-packages/wx-2.8-msw-unicode/wx/_controls.py", line 1850, in AppendText return _controls_.TextCtrl_AppendText(*args, **kwargs) UnicodeDecodeError: 'gbk' codec can't decode byte 0x91 in position 2: incomplete multibyte sequence >>> print u.encode('gbk') Traceback (most recent call last): File "<pyshell#42>", line 1, in <module> print u.encode('gbk') UnicodeEncodeError: 'gbk' codec can't encode character u'/uc911' in position 0: illegal multibyte sequence >>>
>>> a = wx.App() >>> f = wx.Frame(None,-1,'test') >>> t = wx.TextCtrl(f,-1) >>> t.SetValue('일') Unsupported characters in input >>> t.SetValue('中'.decode('gbk')+unichr(0xc911)+'/xec/xa4/x91'.decode('utf8')) >>> f.Show()
显示
中중중
于是,《汉字大全》可以发表0.7版了。
见http://code.google.com/p/hzdq