本文實例講述了Python設置默認編碼為utf8的方法。分享給大家供大家參考,具體如下:
這是Python的編碼問題,設置python的默認編碼為utf8
python安裝目錄:/etc/python2.x/sitecustomize.py
import sysreload(sys)sys.setdefaultencoding('utf-8')try: import apport_python_hookexcept ImportError: passelse: apport_python_hook.install()如果在windows下:
可以在Python安裝目錄下的Lib/site-packages目錄中,新建一個sitecustomize.py文件(也可以建在其它地方,然后手工導入,建在這里,每次啟動Python的時候設置將自動生效),內容如下:
import syssys.setdefaultencoding('utf-8') #set default encoding to utf-8然后可以查看到改變已經生效
>>> import sys>>> sys.getdefaultencoding()'utf-8'
此時運行程序,如果仍然報告之前的錯誤,只需要顯示地設定輸出的編碼
print s.encode('utf-8')就可以看到正確顯示。
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選