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)

原文地址:https://www.cnblogs.com/onelikeone/p/12826528.html