Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can encode('ascii', 'ignore') throw a UnicodeDecodeError?

This line

data = get_url_contents(r[0]).encode('ascii', 'ignore')

produces this error

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 11450: ordinal not in range(128)

Why? I assumed that because I'm using 'ignore' that it should be impossible to have decode errors when saving the output to a value to a string variable.

like image 547
Trindaz Avatar asked Oct 01 '11 22:10

Trindaz


People also ask

What does UnicodeDecodeError mean in Python?

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 .

What is UnicodeEncodeError?

The UnicodeEncodeError normally happens when encoding a unicode string into a certain coding. Since codings map only a limited number of unicode characters to str strings, a non-presented character will cause the coding-specific encode() to fail. Encoding from unicode to str. >>>

What does .encode do in Python?

The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.


1 Answers

Due to a quirk of Python 2, you can call encode on a byte string (i.e. text that's already encoded). In this case, it first tries to convert it to a unicode object by decoding with ascii. So, if get_url_contents is returning a byte string, your line effectively does this:

get_url_contents(r[0]).decode('ascii').encode('ascii', 'ignore')

In Python 3, byte strings don't have an encode method, so the same problem would just cause an AttributeError.

(Of course, I don't know that this is the problem - it could be related to the get_url_contents function. But what I've described above is my best guess)

like image 181
Thomas K Avatar answered Oct 07 '22 04:10

Thomas K