Categories
Python Answers

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

Spread the love

Sometimes, we want to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python.

In this article, we’ll look at how to fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python.

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

To fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python, we can call open with the encoding argument.

For instance, we write

with open(csv_name_here, 'r', encoding="utf-8") as f:
    #...

to call open with the file path and the encoding argument set to 'utf-8' to open the CSV as a Unicode document.

This will make open decode the Unicode characters without errors.

Conclusion

To fix UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128) in Python, we can call open with the encoding argument.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *