python 中文编码解决手记

提示

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 22: ordinal not in range(128)

这个错误相当经典,从数据库里读取有中英混合的数据时交给django rest_framwork render时出错。
最后用了下面的代码解决,先判断是不是unicode,如果不是,则转换为unicode

if not isinstance(_str, unicode):
    data = unicode(_str,'utf-8')
else:
    data = _str

你可能感兴趣的:(python 中文编码解决手记)