注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Linux >> Linux编程 >> Python编程 >> Linux正文

python 中文解决方法 gb2312 <==> utf8

文章来源:linux宝库 作者:quijote 更新时间:2007-1-3 【 】 【加入收藏

程序包见附件
也可参考
http://quijote.blog@bbs.nju.edu.cn


作 者: quijote


抛砖引玉

这是我以前收集整理的。内容比较凌乱,也比较全面。
包括windows, python2.3,pyqt. 而pygtk和thinker和pyqt类似都用unicode.


我想最好的办法是做一个库直接调用gb13080编码字库.
我搜集了一个gb18030映射表 > 830k, 这样双向两个表 > 1.6 M


在win2000+sp3,python2.2

from Tkinter import *
w = Button(text="中国".decode("mbcs"), font="simhei", command='exit')
w.pack()
w.mainloop()
这个方法治标不治本
有时候,我会把字符串的mbcs(GB)和unicode混淆



这个方法有个缺点,由于mbcs的缘故,只适用于windows系统.
一个解决办法,安装
http://sourceforge.net/projects/python-codecs/
A SourceForge project working on additional support for Asian codecs for use
with Python. They are in the early stages of development at the time of this
writing -- look in their FTP area for downloadable files.
(见 Python Library Reference 4.9)
略作修改即可使用

下载4个文件
eucgb23212utf.py (182K) ,
utf2eucgb2321.py (182K),
( http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python-
codecs/practicecodecs/ChineseCodecs/chinesecn/Attic/ )
eucgb2321_cn.py (
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python-
codecs/practicecodecs/ChineseCodecs/Python/)
test.py

本来有个setup.py, 但我不会用,手工修改:

1.把EUCGB2321_CN 替换成gb2312,包括文件名,文件里面的内容;

2. aliases.py 文件最后添加一行
# eucgb2321_cn codec
'gb2312' : 'gb2312',

3. 需要:c:\python22\lib\encodings中,新建一个目录chinesecn,
放置gb23122utf.py (182K) ,utf2gb2312.py (182K),
和 __init__.py(文件内容为空)三个文件,

4. encodings下,放置gb2312.py文件(原名是eucgb2321_cn.py ?)

)。

注释(2003.7):
EUCGB2321_CN 是unix下汉字编码。

直接下载:
http://bbs1.nju.edu.cn/file/gb2312.rar
即可。

------------------------------------------------------------------------
运行 test.py

gbstring = "大家好"
#print gbstring

uni = unicode(gbstring, "gb2312")

gstring = uni.encode("gb2312")

print "Original gb2312 encoded string:"
print gbstring
print "Transcode to Unicode encoding:"
print repr(uni)
print "Print as a gb2312 encoded string:"
print gstring

------------------------------------------------------------
运行结果:
Original gb2312 encoded string:
大家好
Transcode to Unicode encoding:
u'\u5927\u5bb6\u597d'
Print as a gb2312 encoded string:
大家好
------------------------------------------------------------------------------
这个方法的缺点,有点麻烦(unicode(gbstring, "gb2312")),
只适用gb2312,而不是gb18030编码(没有unicode<-->gb18030 table)
我搜集了一个gb18030映射表 > 830k, 这样双向两个表 > 1.6 M

优点是 通用性很好,无论windows, linux系统,还是
Tkinter, pyQT, pyGTK, wxpython都可以使用。

---------------------------------------------------------------------------
btw,
eucgb2321, 2321? 2312? 把我搞迷糊了 ^_^
EUCGB2321_CN 是unix下汉字编码。

我原本用杜文山先生的汉化包( http://dohao.org),可是他并不能及时更新了,
只好另想办法。



python 开发人员的建议

寄件者:Martin v. Loewis (martin@v.loewis.de)
主旨:Re: Chinese language support of Python?

View this article only
新闻群组:comp.lang.python
日期:2002-07-07 01:01:02 PST



guidance_shanghai@yahoo.com.cn (Leon Wang) writes:

> But still can not put Chinese directly as string in source, I can not
> live with so much \u... for a whole Chinese sensence/paragraph, it's
> impossible to read and edit them

This is a known problem, and it will be addressed with PEP 263
(http://www.python.org/peps/pep-0263.html.

Meanwhile, you have the following options:

- Don't use IDLE to edit Python source code (but, say, notepad), and
only put Chinese text into string literals.
- Set the default encoding in site.py to the encoding you want to use.
- Apply patch
http://sourceforge.net/tracker/index.php?func=detail&aid=508973&group_id=957
9&atid=309579

which allows you to declare the source encoding for IDLE.

In either case, you cannot use Chinese in Unicode literals. Instead,
you should always use

unicode("chinese string", "chinese encoding")

For portability, and if your editors support it, I recommend to use
UTF-8 as the "chinese encoding".

Regards,
Martin


  • 上一篇Linux:

  • 下一篇Linux:
  • 最 新 热 门
     VMware下Linux与真实主机共享上网
     Linux系统安全隐患及加强安全管理方法
     各类Unix和Linux密码丢失解决方法
     配置Linux操作系统的环境变量
     在Linux系统下刷新主板BIOS的好方法
     删除文件提示:Operation not permitted
     Linux系统下硬盘挂载详细说明
     Linux文本模式下如何发送带附件的Email
     在Linux自动生成一个保险密码的方法
     Linux商业电影动画制作软件列表
    最 新 推 荐
     Python anygui 项目预览
     Python 之优雅与瑕疵
     python 中文解决方法 gb2312 <==> utf8
     Python anygui 项目预览
     使用TurboGears和Python开发Web 站点
     Python编程技巧-使用状态机
     Curses 编程 -- 给初学者的提示
     概说Python编程
     无所不能的python
     简明 Python 教程
    相 关 文 章

    Linux系统下设置环境变量Path的方法
    Rhythmbox与Totem 不支持MP3解决方法
    Python anygui 项目预览
    PHP5中的this,self和parent关键字详解
    分页显示详解(with php)
    PHP5的异常处理机制之使用throw关键字
    Mythbuntu 7.10正式发布Ubuntu媒体中心版
    即时科研推出Thiz BEST Linux人才认证体系
    Python 之优雅与瑕疵
    Python anygui 项目预览

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用