0
0
PythonDebug / FixBeginner · 3 min read

How to Fix Unicode Decode Error in Python Quickly

A UnicodeDecodeError in Python happens when you try to read text with the wrong encoding. To fix it, specify the correct encoding when opening files, like encoding='utf-8'. This tells Python how to properly convert bytes to text.
🔍

Why This Happens

This error occurs because Python tries to read bytes as text but uses the wrong character encoding. For example, if a file contains special characters saved in UTF-8 but Python reads it as ASCII, it can't decode some bytes and throws UnicodeDecodeError.

python
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)
Output
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
🔧

The Fix

To fix this, tell Python the correct encoding when opening the file. Most modern text files use UTF-8 encoding. Add encoding='utf-8' to the open() function to decode bytes properly.

python
with open('example.txt', 'r', encoding='utf-8') as file:
    content = file.read()
    print(content)
Output
This is a sample text with special characters: café, naïve, résumé.
🛡️

Prevention

Always specify the encoding when reading or writing text files to avoid this error. Use encoding='utf-8' unless you know the file uses a different encoding. Also, avoid mixing byte and string operations without clear conversions.

Use tools or editors that save files in UTF-8 by default. If you handle data from unknown sources, catch exceptions and try different encodings.

⚠️

Related Errors

Similar errors include UnicodeEncodeError, which happens when Python tries to convert text to bytes with the wrong encoding. Another is UnicodeError, a general error for encoding/decoding issues. Fixes usually involve specifying the correct encoding or decoding method.

Key Takeaways

Always specify the correct encoding when opening text files in Python.
UTF-8 is the most common encoding and usually the safe choice.
UnicodeDecodeError means Python tried to read bytes as text but failed due to wrong encoding.
Use exception handling to manage files with unknown or mixed encodings.
Avoid mixing bytes and strings without explicit encoding or decoding.