问题描述:
Python2调用xlwt库的报错,报错提示“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)”编码问题。
原因分析:
xlwt库中在保存excel文件是会调用UnicodeUtils组件的upack1和upack2函数,传入的encoding参数应该是utf-8,但由于开发者忽略了这个参数 没有正确的传入,导致一直传入默认值ascii,应该是bug。
解决方案:
在xlwt库python代码UnicodeUtils.py中添加指定encoding参数是utf-8即可。
]# vim /usr/lib/python2.7/site-packages/xlwt-1.3.0-py2.7.egg/xlwt/UnicodeUtils.py
def upack2(s, encoding='ascii'): if encoding == "ascii": #添加代码 encoding = "utf-8" # 添加代码 # If not unicode, make it so. if isinstance(s, unicode_type): def upack1(s, encoding='ascii'): if encoding == "ascii": # 添加代码 encoding = "utf-8" # 添加代码 # Same as upack2(), but with a one-byte length field. if isinstance(s, unicode_type): us = s
标签:ordinal,xlwt,utf,encoding,报错,byte,ascii From: https://www.cnblogs.com/hraa0101/p/16869792.html