Python設定系統預設編碼

2021-08-14 13:38:42 字數 625 閱讀 3728

python預設的系統編碼為ascii編碼,在日程的編碼中經常會碰到python的編碼問題,為了一勞永逸的解決這個問題,可以將python的系統預設編碼設定為utf-8

unicodeencodeerror: 'ascii' codec can't encode characters in position
import sys

sys.getdefaultencoding()

import sys  

sys.setdefaultencoding('utf8')

anaconda目錄通常為yourpath/anaconda/lib/python2.7/site-packages

mac目錄通常為/usr/local/lib/python2.7/site-packages

import io

import sys

# or

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

Python 設定系統預設編碼

python在安裝時,預設的編碼是ascii,當程式中出現非ascii編碼時,python的處理常常會報這樣的錯unicodedecodeerror ascii codec can t decode byte 0x?in position 1 ordinal not in range 128 pyt...

Python 設定系統預設編碼

實際程式設計中必定要會遇到編碼問題。python在安裝時,預設的編碼是ascii,當程式中出現非ascii編碼時,python的處理常常會報這樣的錯unicodedecodeerror ascii codec can t decode byte 0x?in position 1 ordinal no...

Python 設定系統預設編碼

1.coding utf 8 作用是定義源 的編碼.如果沒有定義,此原始碼中是不可以包含中文字串的.pep 0263 defining python source code encodings 2.sys.getdefaultencoding 是設定預設的string的編碼格式 python在安裝時...