524

Why is the below item failing? Why does it succeed with "latin-1" codec?

o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving v = o.decode("utf-8") 

Which results in:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte 

13 Answers 13

600

I had the same error when I tried to open a CSV file by pandas.read_csv method.

The solution was change the encoding to latin-1:

pd.read_csv('ml-100k/u.item', sep='|', names=m_cols , encoding='latin-1') 
Sign up to request clarification or add additional context in comments.

2 Comments

Does this actually solve the problem though? Doesn't it basically just tell pandas to ignore the byte by downgrading to a less complex encoding style?
Works well for builtin open function. Thanks
369

In binary, 0xE9 looks like 1110 1001. If you read about UTF-8 on Wikipedia, you’ll see that such a byte must be followed by two of the form 10xx xxxx. So, for example:

>>> b'\xe9\x80\x80'.decode('utf-8') u'\u9000' 

But that’s just the mechanical cause of the exception. In this case, you have a string that is almost certainly encoded in latin 1. You can see how UTF-8 and latin 1 look different:

>>> u'\xe9'.encode('utf-8') b'\xc3\xa9' >>> u'\xe9'.encode('latin-1') b'\xe9' 

(Note, I'm using a mix of Python 2 and 3 representation here. The input is valid in any version of Python, but your Python interpreter is unlikely to actually show both unicode and byte strings in this way.)

2 Comments

Thanks (and to the other that replied), I was under the mistaken belief that chars up until 255 would directly convert.
I get UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-3: ordinal not in range(128) error on using .encode(latin-1)
77

It is invalid UTF-8. That character is the e-acute character in ISO-Latin1, which is why it succeeds with that codeset.

If you don't know the codeset you're receiving strings in, you're in a bit of trouble. It would be best if a single codeset (hopefully UTF-8) would be chosen for your protocol/application and then you'd just reject ones that didn't decode.

If you can't do that, you'll need heuristics.

1 Comment

And for heuristics, see the chardet library.
55

Because UTF-8 is multibyte and there is no char corresponding to your combination of \xe9 plus following space.

Why should it succeed in both utf-8 and latin-1?

Here how the same sentence should be in utf-8:

>>> o.decode('latin-1').encode("utf-8") 'a test of \xc3\xa9 char' 

1 Comment

Latin-1 is a single byte encoding family so everything in it should be defined in UTF-8. But why sometime Latin-1 wins?
33

Use this, If it shows the error of UTF-8

pd.read_csv('File_name.csv',encoding='latin-1') 

Comments

31

If this error arises when manipulating a file that was just opened, check to see if you opened it in 'rb' mode

3 Comments

Thanks to this answer, was able to avoid the error of, UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 2024079: invalid continuation byte by soup = BeautifulSoup(open('webpage.html', 'rb'), 'html.parser')
This was also the solution for the same problem when using trimesh.util.concatenate()'s export method.
got this working with png and jpg files
22

utf-8 code error usually comes when the range of numeric values exceeding 0 to 127.

the reason to raise this exception is:

1)If the code point is < 128, each byte is the same as the value of the code point. 2)If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.)

In order to to overcome this we have a set of encodings, the most widely used is "Latin-1, also known as ISO-8859-1"

So ISO-8859-1 Unicode points 0–255 are identical to the Latin-1 values, so converting to this encoding simply requires converting code points to byte values; if a code point larger than 255 is encountered, the string can’t be encoded into Latin-1

when this exception occurs when you are trying to load a data set ,try using this format

df=pd.read_csv("top50.csv",encoding='ISO-8859-1') 

Add encoding technique at the end of the syntax which then accepts to load the data set.

1 Comment

Hi and welcome to SO! Please edit your answer to ensure that it improves upon other answers already present in this question.
13

Well this type of error comes when u are taking input a particular file or data in pandas such as :-

data=pd.read_csv('/kaggle/input/fertilizers-by-product-fao/FertilizersProduct.csv) 

Then the error is displaying like this :- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 1: invalid continuation byte

So to avoid this type of error can be removed by adding an argument

data=pd.read_csv('/kaggle/input/fertilizers-by-product-fao/FertilizersProduct.csv', encoding='ISO-8859-1') 

2 Comments

Please format your code properly, click here to learn how.
It works but it does not gets some country names from FAO data
12

This happened to me also, while i was reading text containing Hebrew from a .txt file.

I clicked: file -> save as and I saved this file as a UTF-8 encoding

1 Comment

I have a problem, i'm trying to set up Odoo 19, and I don't have any custom modules yet. I don't know if it's a library issue, but I've already installed all the libraries listed in the requirements.txt file. Error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf3 in position 85: invalid continuation byte
7

TLDR: I would recommend investigating the source of the problem in depth before switching encoders to silence the error.

I got this error as I was processing a large number of zip files with additional zip files in them.

My workflow was the following:

  1. Read zip
  2. Read child zip
  3. Read text from child zip

At some point I was hitting the encoding error above. Upon closer inspection, it turned out that some child zips erroneously contained further zips. Reading these zips as text lead to some funky character representation that I could silence with encoding="latin-1", but which in turn caused issues further down the line. Since I was working with international data it was not completely foolish to assume it was an encoding problem (I had problems with 0xc2: Â), but in the end it was not the actual issue.

Comments

3

In this case, I tried to execute a .py which active a path/file.sql.

My solution was to modify the codification of the file.sql to "UTF-8 without BOM" and it works!

You can do it with Notepad++.

i will leave a part of my code.

con = psycopg2.connect(host = sys.argv[1], port = sys.argv[2],dbname = sys.argv[3],user = sys.argv[4], password = sys.argv[5]) cursor = con.cursor() sqlfile = open(path, 'r') 

Comments

0

I encountered this problem, and it turned out that I had saved my CSV directly from a google sheets file. In other words, I was in a google sheet file. I chose, save a copy, and then when my browser downloaded it, I chose Open. Then, I DIRECTLY saved the CSV. This was the wrong move.

What fixed it for me was first saving the sheet as an .xlsx file on my local computer, and from there exporting single sheet as .csv. Then the error went away for pd.read_csv('myfile.csv')

Comments

-1

The solution was change to "UTF-8 sin BOM"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.