admin管理员组

文章数量:1610901

我的Linux服务器的Python版本是2.*,在将中文encode(UTF-8)的时候出现了UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)的问题。而在3.*中没有问题。


解决方法:
在代码前端加入
import sys
reload(sys)
sys.setdefaultencoding('utf8')

Python 2.x,字符编码方面,设计的不好。字符串包含两种----->str字符串(此时字符串的编码类型,对应着你的Python文件本身保存为何种编码有关)和unicode字符串
而在Python 3.*中,字符串则是unicode编码的str。上面这个问题,就是Python2.*渣编码的一个体现
 

本文标签: ASCIICodecPythonUnicodeDecodeErrorPosition