UnicodeDecodeError,utf-8 无效的继续字节 [英] UnicodeDecodeError, utf-8 invalid continuation byte

查看:74
本文介绍了UnicodeDecodeError,utf-8 无效的继续字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用该代码从日志文件中提取行:

I m trying to extract lines from a log file , using that code :

    with open('fichier.01') as f:
         content = f.readlines()

    print (content)

但它总是使错误声明

    Traceback (most recent call last):
    File "./parsepy", line 4, in <module>
    content = f.readlines()
    File "/usr/lib/python3.5/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2213: invalid continuation byte

我该如何解决?

推荐答案

如果它没有被编码为文本,那么你必须以二进制模式打开它,例如:

If it's not encoded as text then you will have to open it in binary mode e.g.:

with open('fichier.01', 'rb') as f:
    content = f.readlines()

如果它被编码为非 UTF-8 并且可以在文本模式下打开,则 open 接受一个 encoding 参数:https://docs.python.org/3.5/library/functions.html#open

If it's encoded as something other than UTF-8 and it can be opened in text mode then open takes an encoding argument: https://docs.python.org/3.5/library/functions.html#open

这篇关于UnicodeDecodeError,utf-8 无效的继续字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆