Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnicodeDecodeError, utf-8 invalid continuation byte

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

like image 889
Ussopokingo Avatar asked Jun 01 '17 13:06

Ussopokingo


People also ask

What is an invalid continuation byte?

The Python "UnicodeDecodeError: 'utf-8' codec can't decode byte in position: invalid continuation byte" occurs when we specify an incorrect encoding when decoding a bytes object. To solve the error, specify the correct encoding, e.g. latin-1 .

What does UnicodeDecodeError mean?

The Python "UnicodeDecodeError: 'ascii' codec can't decode byte in position" occurs when we use the ascii codec to decode bytes that were encoded using a different codec. To solve the error, specify the correct encoding, e.g. utf-8 . Here is an example of how the error occurs. I have a file called example.


2 Answers

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.

like image 128
MattR Avatar answered Sep 21 '22 12:09

MattR


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

like image 30
grahamlyons Avatar answered Sep 19 '22 12:09

grahamlyons