解決python中文亂碼問(wèn)題方法總結(jié)
在運(yùn)行這樣類似的代碼:
#!/usr/bin/env pythons='中文'print s
最近經(jīng)常遇到這樣的問(wèn)題:
問(wèn)題一:SyntaxError: Non-ASCII character ’xe4’ in file E:codingpythonUntitled 6.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
問(wèn)題二:UnicodeDecodeError: ’ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
問(wèn)題三:UnicodeEncodeError: ’gb2312’ codec can’t encode character u’u2014’ in position 72366: illegal multibyte sequence
這些都是跟字符編碼有關(guān)的問(wèn)題,很郁悶,中文總是弄不出來(lái),找了很多方案,這里有些是我前幾天找到的一些方案,拿出來(lái)給大家分享一下哈
字符串在Python內(nèi)部的表示是unicode 編碼,因此,在做編碼轉(zhuǎn)換時(shí),通常需要以u(píng)nicode作為中間編碼,即先將其他編碼的字符串解碼(decode)成unicode,再?gòu)膗nicode編碼(encode)成另一種編碼。
decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼,如str1.decode(’gb2312’),表示將gb2312編碼的字符串str1轉(zhuǎn)換成unicode編碼。
encode的作用是將unicode編碼轉(zhuǎn)換成其他編碼的字符串,如str2.encode(’gb2312’),表示將unicode編碼的字符串str2轉(zhuǎn)換成gb2312編碼。
在某些IDE中,字符串的輸出總是出現(xiàn)亂碼,甚至錯(cuò)誤,其實(shí)是由于IDE的結(jié)果輸出控制臺(tái)自身不能顯示字符串的編碼,而不是程序本身的問(wèn)題。
如在UliPad中運(yùn)行如下代碼:
s=u'中文'print s
會(huì)提示:
UnicodeEncodeError: ’ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)
這是因?yàn)閁liPad在英文WindowsXP 上的控制臺(tái)信息輸出窗口是按照ascii編碼輸出的(英文系統(tǒng)的默認(rèn)編碼是ascii),而上面代碼中的字符串是Unicode編碼的,所以輸出時(shí)產(chǎn)生了錯(cuò)誤。
將最后一句改為:print s.encode(’gb2312’)
則能正確輸出“中文”兩個(gè)字。
若最后一句改為:print s.encode(’utf8’)
則輸出:xe4xb8xadxe6x96x87,這是控制臺(tái)信息輸出窗口按照ascii編碼輸出utf8編碼的字符串的結(jié)果。
下面代碼可能比較通用一些,如下:
#!/usr/bin/env python #coding=utf-8 s='中文'if isinstance(s,unicode): #s=u'中文' print s.encode(’gb2312’) else: #s='中文' print s.decode(’utf-8’).encode(’gb2312’)#!/usr/bin/env python#coding=utf-8s='中文'if isinstance(s,unicode): #s=u'中文' print s.encode(’gb2312’)else: #s='中文' print s.decode(’utf-8’).encode(’gb2312’)
看看下面一段代碼:
#!/usr/bin/env python #coding=utf-8 #python version:2.7.4 #system:windows xp import httplib2def getPageContent(url): ’’’’’ 使用httplib2用編程的方式根據(jù)url獲取網(wǎng)頁(yè)內(nèi)容 將bytes形式的內(nèi)容轉(zhuǎn)換成utf-8的字符串 ’’’ #使用ie9的user-agent,如果不設(shè)置user-agent將會(huì)得到403禁止訪問(wèn) headers={’user-agent’:’Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)’, ’cache-control’:’no-cache’} if url: response,content= httplib2.Http().request(url,headers=headers) if response.status== 200 : return content
import sys reload(sys) sys.setdefaultencoding(’utf-8’) #修改默認(rèn)編碼方式,默認(rèn)為ascci print sys.getdefaultencoding() content= getPageContent('http://www.oschina.net/')print content.decode(’utf-8’).encode(’gb2312’)#!/usr/bin/env python#coding=utf-8#python version:2.7.4#system:windows xpimport httplib2def getPageContent(url): ’’’ 使用httplib2用編程的方式根據(jù)url獲取網(wǎng)頁(yè)內(nèi)容 將bytes形式的內(nèi)容轉(zhuǎn)換成utf-8的字符串 ’’’ #使用ie9的user-agent,如果不設(shè)置user-agent將會(huì)得到403禁止訪問(wèn) headers={’user-agent’:’Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)’, ’cache-control’:’no-cache’} if url: response,content= httplib2.Http().request(url,headers=headers) if response.status== 200 : return content
import sysreload(sys)sys.setdefaultencoding(’utf-8’) #修改默認(rèn)編碼方式,默認(rèn)為ascciprint sys.getdefaultencoding()content= getPageContent('//www.jb51.net/')print content.decode(’utf-8’).encode(’gb2312’)
上面的代碼的意思:向www.jb51.net網(wǎng)站請(qǐng)求他的主頁(yè),(如果直接是utf-8編碼,不能輸出中文)想將編碼方式為utf-8轉(zhuǎn)向gd2312,出現(xiàn)問(wèn)題三
當(dāng)我把它將print content.decode(’utf-8’).encode(’gb2312’)改成print content.decode(’utf-8’).encode(’gb2312’, ‘ignore’)時(shí),OK了,可以顯示中文了,但不敢確定是否為全部,貌似只有部分吧,有些不能用gb2312編碼
然而,當(dāng)我把網(wǎng)站換成 www.soso.com時(shí),不用轉(zhuǎn)為gb2312,用utf-8即可正常顯示中文
總結(jié)一下:
向文件直接輸出ss會(huì)拋出同樣的異常。在處理unicode中文字符串的時(shí)候,必須首先對(duì)它調(diào)用encode函數(shù),轉(zhuǎn)換成其它編碼輸出。這一點(diǎn)對(duì)各個(gè)環(huán)境都一樣。在Python中,“str”對(duì)象就是一個(gè)字節(jié)數(shù)組,至于里面的內(nèi)容是不是一個(gè)合法的字符串,以及這個(gè)字符串采用什么編碼(gbk, utf-8, unicode)都不重要。這些內(nèi)容需要用戶自己記錄和判斷。這些的限制也同樣適用于“unicode”對(duì)象。要記住“unicode”對(duì)象中的內(nèi)容可絕對(duì)不一定就是合法的unicode字符串,我們很快就會(huì)看到這種情況。在windows的控制臺(tái)上,支持gbk編碼的str對(duì)象和unicode編碼的unicode對(duì)象。
更多關(guān)于解決python中文亂碼問(wèn)題方法總結(jié)的文章請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 詳解瀏覽器的緩存機(jī)制6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器8. xml中的空格之完全解說(shuō)9. 如何在jsp界面中插入圖片10. phpstudy apache開(kāi)啟ssi使用詳解
