zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    图片处理
    define 常量的定义和读取
    curl
    stream_get_contents 和file_get_content的区别
    php flock 文件锁
    字符串函数
    php 常量
    debug_backtrace()
    pathlib模块替代os.path
    Python中对 文件 的各种骚操作
  • 原文地址:https://www.cnblogs.com/onelikeone/p/12826528.html
Copyright © 2011-2022 走看看