對於python.x來說,安裝時系統默認的編碼方式為ascii。因此,若編碼中出現非ascii編碼(如漢字),運行時就會報錯:UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)。
此時就得注意了,一定是程序的某個部分編碼出了問題,最大嫌疑就是漢字。此時不要慌,按我說的做 ^_^:
在你的模塊中加入下面幾行代碼~~~
import sys
reload(sys)
sys.setdefaultencoding('utf8')
好了,問題解決!~
答疑時間:
一般的 sys.setdefaultencoding('utf8') 也就可以了,但你這么直接,系統可能不會接受你:AttributeError: 'module' object has no attribute 'setdefaultencoding'
;
所以,最好事先 reload(sys),重新加載一下這一模塊,保證萬無一失。
對於python3.x來說,官方已經將默認編碼改為utf8,不再存在此類問題。