‘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')