• 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)

  • 相关阅读:
    curl命令学习笔记
    IOS安全测试思维导图
    Hello World
    Hive SQL使用和数据加载的一点总结
    采用最低松弛度优先调度的实时系统调度程序
    MongoDB Map Reduce速度提升20倍的优化宝典
    释放时间不同可中断平行机问题在线算法
    RPC框架系列——Avro
    Solr 3.6.2索引MySQL数据库配置过程
    IKAnalyzer 独立使用 配置扩展词库
  • 原文地址:https://www.cnblogs.com/onelikeone/p/12826528.html
Copyright © 2020-2023  润新知