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

posted @ 2018-09-29 19:52  Adamanter  阅读(563)  评论(0编辑  收藏  举报