python2.7: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
原因一:
环境:python2.7
报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
方案:
data = u"汉" data = "汉" if isinstance(data, unicode): print("unicode") print(str(data.encode('utf-8'))) elif isinstance(data, str): print("str") print(str(data)) else: print(str(data)) data = u"汉" data = "汉" if type(data) is unicode: print("unicode") print(str(data.encode('utf-8'))) elif type(data) is str: print("str") print(str(data)) else: print(str(data))
原因二:
Linux-ubuntu-uwsgi 编码非utf-8
locale
vi /etc/profile
export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN.UTF-8
source /etc/profile
reboot
或者uwsgi
env LANG=zh_CN.UTF-8
env LANGUAGE=zh_CN.UTF-8