Python疑难杂症:SyntaxError: Non-ASCII character Python中文处理问题
python的中文問題一直是困擾新手的頭疼問題,這篇文章將給你詳細(xì)地講解一下這方面的知識。當(dāng)然,幾乎可以確定的是,在將來的版本中,python會徹底解決此問題,不用我們這么麻煩了。
先來看看python的版本:
>>>?sys.version
'2.5.1?(r251:54863,?Apr?18?2007,?08:51:08)?[MSC?v.1310?32?bit?(Intel)]'
(一)用記事本創(chuàng)建一個文件ChineseTest.py,默認(rèn)ANSI:
print?s
測試一下瞧瞧:
File?"ChineseTest.py",?line?1
SyntaxError:?Non-ASCII?character?'\xd6'?in?file?ChineseTest.py?on?line?1,?but?noencodingdeclared;?see?http://www.pytho
n.org/peps/pep-0263.html?for?details
偷偷地把文件編碼改成UTF-8:
File?"ChineseTest.py",?line?1
SyntaxError:?Non-ASCII?character?'\xe4'?in?file?ChineseTest.py?on?line?1,?but?noencodingdeclared;?see?http://www.pytho
n.org/peps/pep-0263.html?for?details
無濟于事。。。
既然它提供了網(wǎng)址,那就看看吧。簡單地瀏覽一下,終于知道如果文件里有非ASCII字符,需要在第一行或第二行指定編碼聲明。把ChineseTest.py文件的編碼重新改為ANSI,并加上編碼聲明:
s?=?"中文"
print?s
再試一下:
中文
正???#xff1a;)
(二)看一看它的長度:
s?=?"中文"
print?len(s)
結(jié)果:4。
s這里是str類型,所以計算的時候一個中文相當(dāng)于兩個英文字符,因此長度為4。
我們這樣寫:
s?=?"中文"
s1?=?u"中文"
s2?=?unicode(s,?"gbk")?#省略參數(shù)將用python默認(rèn)的ASCII來解碼
s3?=?s.decode("gbk")?#把str轉(zhuǎn)換成unicode是decode,unicode函數(shù)作用與之相同
print?len(s1)
print?len(s2)
print?len(s3)
結(jié)果:
2
2
2
(三)接著來看看文件的處理:建立一個文件test.txt,文件格式用ANSI,內(nèi)容為:abc中文,用python來讀取
print?open("Test.txt").read()
結(jié)果:abc中文
把文件格式改成UTF-8:
結(jié)果:abc涓 枃
顯然,這里需要解碼:
import?codecs
print?open("Test.txt").read().decode("utf-8")
結(jié)果:abc中文
上面的test.txt我是用Editplus來編輯的,但當(dāng)我用Windows自帶的記事本編輯并存成UTF-8格式時,
運行時報錯:
import?codecs
print?open("Test.txt").read().decode("utf-8")
原來,某些軟件,如notepad,在保存一個以UTF-8編碼的文件時,會在文件開始的地方插入三個不可見的字符(0xEF 0xBB 0xBF,即BOM)。
因此我們在讀取時需要自己去掉這些字符,python中的codecs module定義了這個常量:
import?codecs
print?open("Test.txt").read().decode("utf-8")
結(jié)果:abc中文
(四)一點遺留問題
在第二部分中,我們用unicode函數(shù)和decode方法把str轉(zhuǎn)換成unicode。為什么這兩個函數(shù)的參數(shù)用"gbk"呢?
第一反應(yīng)是我們的編碼聲明里用了gbk(# coding=gbk),但真是這樣?
修改一下源文件:
s?=?"中文"
print?unicode(s,?"utf-8")
運行,報錯:
File?"ChineseTest.py",?line?3,?in?<module>
????s?=?unicode(s,?"utf-8")
UnicodeDecodeError:?'utf8'?codec?can't?decode?bytes?in?position?0-1:?invalid?data
顯然,如果前面正常是因為兩邊都使用了gbk,那么這里我保持了兩邊utf-8一致,也應(yīng)該正常,不至于報錯。
更進一步的例子,如果我們這里轉(zhuǎn)換仍然用gbk:
s?=?"中文"
print?unicode(s,?"gbk")
結(jié)果:中文
翻閱了一篇英文資料,它大致講解了python中的print原理:
When Python executes a print statement, it simply passes the output to the operating system (using fwrite() or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be the Windows console subsystem that displays the result. Or if you're using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then xterm and your X server handle the display.
To print data reliably, you must know the?encoding?that this display program expects.
簡單地說,python中的print直接把字符串傳遞給操作系統(tǒng),所以你需要把str解碼成與操作系統(tǒng)一致的格式。Windows使用CP936(幾乎與gbk相同),所以這里可以使用gbk。
最后測試:
s?=?"中文"
print?unicode(s,?"cp936")
轉(zhuǎn)載于:https://www.cnblogs.com/icamel/archive/2012/05/24/2516546.html
總結(jié)
以上是生活随笔為你收集整理的Python疑难杂症:SyntaxError: Non-ASCII character Python中文处理问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 只用一套解决方案,就可解决80%的交通物
- 下一篇: python脚本调用外部程序的若干种方式