UnicodeDecodeError: 'ascii' codec can't decode byte错误解决方案

Posted onelikeone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnicodeDecodeError: 'ascii' codec can't decode byte错误解决方案相关的知识,希望对你有一定的参考价值。

 

异常信息如下:

sh-4.2$ python begin.py
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 874, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in position 82: ordinal not in range(128)
Logged from file google.py, line 95
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 874, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in position 88: ordinal not in range(128)
Logged from file google.py, line 125
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 874, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in position 89: ordinal not in range(128)
Logged from file google.py, line 142
Traceback (most recent call last):
File "/usr/lib64/python2.7/logging/__init__.py", line 874, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in position 89: ordinal not in range(128)
Logged from file google.py, line 159

 


本次错误信息出现在logging代码中,python2的解决的办法是,在开头添加如下代码:

import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)


python3.x下应该改为如下方式:

import importlib
importlib.reload(sys)

以上是关于UnicodeDecodeError: 'ascii' codec can't decode byte错误解决方案的主要内容,如果未能解决你的问题,请参考以下文章

python编码问题:UnicodeDecodeError: 'gbk' codec can't decode

python UnicodeDecodeError: 'ascii' codec can't decode byte ...

UnicodeDecodeError: 'ascii' codec can't decode byte错误解决方案

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd

UnicodeDecodeError: 'utf-8' codec can't decode byte..问题

UnicodeDecodeError: 'utf-8' codec can't decode byte..问题