歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> python 中文解決方法 gb2312 utf8

python 中文解決方法 gb2312 utf8

日期:2017/2/27 14:29:09   编辑:更多Linux
  程序包見附件 也可參考 http://[email protected] 作 者: quijote 標 題: python程序中的中文字符處理(2003.7.11) 時 間: Wed Jun 11 10:47:43 2003 點 擊: 22 拋磚引玉 這是我以前收集整理的。內容比較凌亂,也比較全面。 包括windows, python2.3,pyqt. 而pygtk和thinker和pyqt類似都用unicode. 我想最好的辦法是做一個庫直接調用gb13080編碼字庫. 我搜集了一個gb18030映射表 > 830k, 這樣雙向兩個表 > 1.6 M ZZ from Linuxforum 文章標題 剛學了一招。 [re: wang_jianqiang] 回復 張貼者: xlp223 (newbie) 張貼日期 01/13/03 09:56 在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 ([email protected]) 主旨:Re: Chinese language support of Python? View this article only 新聞群組:comp.lang.python 日期:2002-07-07 01:01:02 PST [email protected] (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 又一個例子, 在python2.3a1下可以運行 不再用 .encode("gb2312")了

看來python2.3對unicode的支持真的有很大改進 這個看來是目前最好的解決方法 !!!注意: 編輯器使用utf-8編碼, 此類文本文件一般以 FF FE 開頭,在python2.2下不能運行! 經人提醒, 知道可以使用windows font exunicode.py # -*- coding: utf-8 -*- from Tkinter import * w = Button(text="大家好",font=("SIMSUN",8,'bold'), command='exit') w.pack() w.mainloop() 3 PEP 263: Source Code Encodings Python source files can now be declared as being in different character set 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 only affects Unicode string literals; the text in the source code will be converted to Unicode using the specified encoding. Note that Python identifiers are still restricted to ASCII characters, so you can't have variable names that use characters outside of the usual alphanumerics. 我剛學習使用pyQt,使用win2k, 安裝了Du Wenshan兄的中文 mbcsp包, http://dohao.org 一些練習: >>> u"我們" u'\xce\xd2\xc3\xc7' >>> u'阿啊' u'\xb0\xa2\xb0\xa1' #注意此處不是unicode碼,而是 gb2312.. 1. 漢字區。包括:   a. GB 2312 漢字區。即 GBK/2: B0A1-F7FE。收錄 GB 2312 漢字 6763 個,按原順序排列。 b. GB 13000.1 擴充漢字區。包括: (1) GBK/3: 8140-A0FE。收錄 GB 13000.1 中的 CJK 漢字 6080 個。 (2) GBK/4: AA40-FEA0。收錄 CJK 漢字和增補的漢字 8160 個。CJK 漢字 在前,按 UCS 代碼大小排列;增補的漢字(包括部首和構件)在後,按《康熙 字典》的頁碼/字位排列。 這也許是簡體中文版win2k的原故, 我猜想多國語言版的win2k不會有這樣問題。 幸好, >>> s="我們" >>> unicode(s) u'\u6211\u4eec' 以下是pyQt的程序: from qt import QString s="A string that contains just ASCII characters" u=u"\u963f\u554a - a string with a few chinese characters" qs=QString(s) qu=QString(u) print str(qs) print str(qu) 輸出結果: >C:\Python22\pythonw -u unicode1.py A string that contains just ASCII characters 阿啊 - a string with a few chinese characters >Exit code: 0 改進的方法: from qt import QString s="A string that contains just ASCII characters" #u=u"\u963f\u554a - a string with a few chinese characters" u1="我們 a string with a few chinese characters" #u=unicode(u1) qs=QString(s) qu=QString(unicode("我們--a string with a few chines" )) print str(qs) print str(qu) 輸出結果: >C:\Python22\pythonw -u unicode1.py

A string that contains just ASCII characters 我們--a string with a few chines >Exit code: 0 另外,使用qt designer設計界面,生成*.ui文件,此文件為utf-8格式 利用python目錄下qtuic.exe轉換成python程序。 另外,Wenshan兄的補丁中,不知為什麼,好像缺少sys.setappdefaultencoding()? 附錄: 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,只要按照其中的步驟一步一步執行完就可以了。第二種方法分為 三步進行,如下:    1、下載 python22.dll ,替換原來的同名文件,一般位於Windows安裝目錄裡邊的 system/system32文件夾裡邊。替換完成後,運行python ,你會看到窗口上方增加了一行文 字:      "With MultiByte Character Surport Surplied by dohao.org"   這表示你的python已經開始支持多字節字符了。    2、下載 site.py,替換python安裝目錄\lib裡邊的同名文件。這是為了在一些應用 程序裡邊支持多字節字符,例如IDLE.    3、如果你經常使用IDLE, 下載OutputWindow.py,IOBinding.py,替換Python安裝目 錄\tools\idle裡邊的同名文件。這樣,當你使用IDLE時就會正常顯示多字節字符了。   注意,安裝後,在Tkinter裡邊這樣顯示漢字: Tkinter.Label(text=unicode("中文漢字"))   以上的文件是針對Windows系統的。當你安裝完成後,就可以用多字節字符給你的變量 名稱、類名稱、函數名稱等命名了。當你顯示數據庫裡邊的多字節字符時,就會顯示正常 了。如果你需要針對linux 系統的文件,或者是python 2.1或更早的版本,請告訴我,我將 在這裡加進來。 新:MBCSP100-py213.zip 英文版



Tkinter.Label(text=unicode("中文漢字"))   以上的文件是針對Windows系統的。當你安裝完成後,就可以用多字節字符給你的變量 名稱、類名稱、函數名稱等命名了。當你顯示數據庫裡邊的多字節字符時,就會顯示正常 了。如果你需要針對linux 系統的文件,或者是python 2.1或更早的版本,請告訴我,我將 在這裡加進來。 新:MBCSP100-py213.zip 英文版



  注意,安裝後,在Tkinter裡邊這樣顯示漢字: Tkinter.Label(text=unicode("中文漢字"))   以上的文件是針對Windows系統的。當你安裝完成後,就可以用多字節字符給你的變量 名稱、類名稱、函數名稱等命名了。當你顯示數據庫裡邊的多字節字符時,就會顯示正常 了。如果你需要針對linux 系統的文件,或者是python 2.1或更早的版本,請告訴我,我將 在這裡加進來。 新:MBCSP100-py213.zip 英文版



Copyright © Linux教程網 All Rights Reserved