pythonutf8:python 中文解决方法 gb2312 <==> utf8来源: 发布时间:星期四, 2009年2月12日 浏览:53次 评论:0
包见附件 也可参考 http://[email protected] 作 者: quijote 抛砖引玉 这是我以前收集整理内容比较凌乱也比较全面 包括windows, python2.3,pyqt. 而pygtk和thinker和pyqt类似都用unicode. 我想最好办法是做个库直接gb13080编码字库. 我搜集了个gb18030映射表 > 830k, 这样双向两个表 > 1.6 M 在win2000+sp3,python2.2 from Tker import * w = Button(text="中国".decode("mbcs"), font="simhei", command='exit') w.pack w.loop 这个思路方法治标不治本 有时候我会把串mbcs(GB)和unicode混淆 这个思路方法有个缺点由于mbcs缘故只适用于windows系统. 个解决办法安装 http://sourceforge.net/projects/python-codecs/ A SourceForge project working _disibledevent=>#pr gb uni = unicode(gb, "gb2312") g = uni.encode("gb2312") pr "Original gb2312 encoded :" pr gb pr "Transcode to Unicode encoding:" pr repr(uni) pr "Pr as a gb2312 encoded :" pr g ------------------------------------------------------------ 运行结果: Original gb2312 encoded : 大家好 Transcode to Unicode encoding: u'\u5927\u5bb6\u597d' Pr as a gb2312 encoded : 大家好 ------------------------------------------------------------------------------ 这个思路方法缺点有点麻烦(unicode(gb, "gb2312")) 只适用gb2312,而不是gb18030编码(没有unicode<-->gb18030 table) 我搜集了个gb18030映射表 > 830k, 这样双向两个表 > 1.6 M 优点是 通用性很好,无论windows, 系统,还是 Tker, pyQT, pyGTK, wxpython都可以使用 --------------------------------------------------------------------------- btw, eucgb2321, 2321? 2312? 把我搞迷糊了 ^_^ EUCGB2321_CN 是unix下汉字编码 我原本用杜文山先生汉化包( http://dohao.org)可是他并不能及时更新了, 只好另想办法 python 开发人员建议 寄件者:Martin v. Loewis ([email protected]) 主旨:Re: Chinese language support of Python? View this article _disibledevent=>9&atid=309579 which allows you to declare the source encoding for IDLE. In either , you cannot use Chinese in Unicode literals. Instead, you should always use unicode("chinese ", "chinese encoding") For portability, and your editors support it, I recommend to use UTF-8 as the "chinese encoding". Regards, Martin 又个例子, 在python2.3a1下可以运行 不再用 .encode("gb2312")了 看来python2.3对unicode支持真有很大改进 这个看来是目前最好解决思路方法 !!!注意: 编辑器使用utf-8编码 此类文本文件般以 FF FE 开头在python2.2下不能运行! 经人提醒 知道可以使用windows font exunicode.py # -*- coding: utf-8 -*- from Tker import * w = Button(text="大家好",font=("SIMSUN",8,'bold'), command='exit') w.pack w.loop 3 PEP 263: Source Code Encodings Python source files can now be declared as being in dferent character encodings. Encodings are declared by including a specially formatted comment in the first or second line of the source file. For example, a UTF-8 file can be declared with: #!/usr/bin/env python # -*- coding: UTF-8 -*- Without such an encoding declaration, the default encoding used is ISO-8859-1, also known as Latin1. The encoding declaration _disibledevent=>>>> unicode(s) u'\u6211\u4eec' 以下是pyQt: from qt import QString s="A that contains just ASCII characters" u=u"\u963f\u554a - a with a few chinese characters" qs=QString(s) qu=QString(u) pr str(qs) pr str(qu) 输出结果: >C:\Python22\pythonw -u unicode1.py A that contains just ASCII characters 阿啊 - a with a few chinese characters >Exit code: 0 改进思路方法: from qt import QString s="A that contains just ASCII characters" #u=u"\u963f\u554a - a with a few chinese characters" u1="我们 a with a few chinese characters" #u=unicode(u1) qs=QString(s) qu=QString(unicode("我们--a with a few chines" )) pr str(qs) pr str(qu) 输出结果: >C:\Python22\pythonw -u unicode1.py A that contains just ASCII characters 我们--a with a few chines >Exit code: 0 另外使用qt designer设计界面生成*.ui文件此文件为utf-8格式 利用python目录下qtuic.exe转换成python 另外Wenshan兄补丁中不知为什么好像缺少sys.appdefaultencoding? 附录: Python 多字节支持补充包(MBCSP) 1.0 MBCSP是针对最新python 2.2.1 提供多字节支持补充包目在于彻底解决Python里边多字节显示问题原有Python里边在处理中文、韩文或日文等多字节时常常显示不正常你会经常看到类似于"\xc4\xe3\xba\xc3"这样尤其是处理数据库时经常看到这样使得观察结果显得很不方便尽管不是操作我对Python2.2.1源文件进行了编辑处理形成了MBCSP 1.0.它完全兼容Python2.2.1对其处理能力进行了加强 MBCSP安装思路方法有两种都要求你先安装Python2.2.1.如果你想运行安装可以mbcsp100-py221.exe只要按照其中步骤步步执行完就可以了第 2种思路方法分为 3步进行如下: 1、下载 python22.dll 替换原来同名文件般位于安装目录里边system/system32文件夹里边替换完成后运行python 你会看到窗口上方增加了行文字:"With MultiByte Character Surport Surplied by dohao.org"这表示你python已经开始支持多字节了 2、下载 site.py替换python安装目录\lib里边同名文件这是为了在些应用里边支持多字节例如IDLE. 3、如果你经常使用IDLE 下载OutputWindow.pyIOBinding.py替换Python安装目录\tools\idle里边同名文件这样当你使用IDLE时就会正常显示多字节了 注意安装后在Tker里边这样显示汉字:Tker.Label(text=unicode("中文汉字")) 以上文件是针对系统当你安装完成后就可以用多字节给你变量名称、类名称、名称等命名了当你显示数据库里边多字节时就会显示正常了如果你需要针对 系统文件或者是python 2.1或更早版本请告诉我我将在这里加进来 新:MBCSP100-py213.zip英文版 0
相关文章
读者评论发表评论 |