Python設定預設編碼為utf8的方法,pythonutf8

來源:互聯網
上載者:User

Python設定預設編碼為utf8的方法,pythonutf8

本文執行個體講述了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')

就可以看到正確顯示。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.