python3 執行出現'ascii' codec can't encode characters in position 10-11: ordinal not in range(128)錯誤解決方案
阿新 • • 發佈:2019-02-18
‘ascii’ codec can’t encode characters in position 10-11: ordinal not in range(128)
最近,部署測試環境時新啟用了一臺Ubuntu,安裝完python3執行寫好的指令碼時報錯,差了下基本屬於編碼問題
可嘗試如下解決:
A subtle problem causing even print to fail is having your environment variables set wrong, eg. here LC_ALL set to "C". In Debian they discourage setting it: Debian wiki on Locale
vim /etc/default/locale
#File generated by update-locale
#LANG="en_US"
LANG="en_US.UTF-8"
LANGUAGE="en_US:"
$ echo $LANG
en_US.utf8
$ echo $LC_ALL
C
$ python -c "print (u'voil\u00e0')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii ' codec can't encode character u'\xe0' in position 4: ordinal not in range(128)
$ export LC_ALL='en_US.utf8'
$ python -c "print (u'voil\u00e0')"
voilà
$ unset LC_ALL
$ python -c "print (u'voil\u00e0')"
voilà
import sys
reload(sys)
sys.setdefaultencoding('utf-8')