Skip to content
Home » UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128)

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128)

To solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) error follow below methods.

ERROR LOG

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128)

How to solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) ?

Refer the given methods to solve the issue.

Method 1:

Unicode is not the same as UTF-8. The latter is simply a representation of the former.

You’re going about it the incorrect way. Because you are reading UTF-8-encoded data, you must convert the UTF-8-encoded String to a Unicode string. Simply replace .encode with .decode and you should be good to go (if your .csv is UTF-8-encoded).

But there’s nothing to be ashamed of. I’m sure three out of every five programmers struggled to understand this at first, if not more )

If your input data is not UTF-8 encoded, you must, of course, use .decode() with the appropriate encoding. If no arguments are provided, Python assumes ASCII, which obviously fails with non-ASCII characters.

Method 2 :

The main cause of the problem is because Python’s default encoding is ASCII. As a result, if the string data to be encoded by encode(‘utf8’) contains a character that is not in the ASCII range, such as ‘agjsabqwbi,’ Python will throw an error since the string is not in the anticipated encoding format.

If you are running a Python version prior to 3.5, a reliable workaround would be to change the default encoding expected by Python to utf8:

import sys
reload(sys)
sys.setdefaultencoding('utf8')
value = string_name.encode('utf8')

Python would be able to predict characters in a string that are outside of the ASCII range in this manner.

However, if you are using Python 3.5 or higher, the reload() function is not available, thus you must solve it using decode, for example.

value = string_name.decode('utf8').encode('utf8')

Hope the above solution works.

Also read : ParseError: not well-formed (invalid token) using cElementTree