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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM