Python UnicodeDecodeError,utf-8 无效的继续字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44309044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:54:12  来源:igfitidea点击:

UnicodeDecodeError, utf-8 invalid continuation byte

pythonpython-3.5

提问by Ussopokingo

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

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

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

    print (content)

but its always makes the error statement

但它总是使错误声明

    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

how can i fix it ??

我该怎么修??

回答by MattR

try one of the following

尝试以下方法之一

open('fichier.01', 'rb')
open('fichier.01', encoding ='utf-8')
open('fichier.01', encoding ='ISO-8859-1')

or also you can use io Module:

或者你也可以使用io 模块

import io
io.open('fichier.01')

This is a common error when opening files when using Python (or any language really). This is an error you will soon learn to catch.

这是使用 Python(或任何语言)打开文件时的常见错误。这是一个你很快就会学会发现的错误。

回答by grahamlyons

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

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

如果它被编码为 UTF-8 以外的其他内容并且可以在文本模式下打开,则open需要一个encoding参数:https: //docs.python.org/3.5/library/functions.html#open