UnicodeDecodeError, utf-8 invalid continuation byte

淺唱寂寞╮ 提交于 2019-12-12 09:42:47

问题


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 ??


回答1:


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 open takes an encoding argument: https://docs.python.org/3.5/library/functions.html#open




回答2:


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:

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.



来源:https://stackoverflow.com/questions/44309044/unicodedecodeerror-utf-8-invalid-continuation-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!