Skip to content

Q&A · Answered

How to handle Unicode encoding errors in Python

asked 9 months, 1 week ago 1995 views 1 answer
44

I'm getting UnicodeDecodeError when reading files or processing text. How do I properly handle encoding in Python?

1 Answer

One accepted
8

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago

Unicode errors are common when dealing with files from different sources. Here's how to handle them:

Specify encoding when opening files

# Always specify encoding
with open('file.txt', 'r', encoding='utf-8') as f:
    content = f.read()

# Handle different encodings
try:
    with open('file.txt', 'r', encoding='utf-8') as f:
        content = f.read()
except UnicodeDecodeError:
    with open('file.txt', 'r', encoding='latin-1') as f:
        content = f.read()

Use error handling

# Ignore errors
with open('file.txt', 'r', encoding='utf-8', errors='ignore') as f:
    content = f.read()

# Replace invalid characters
with open('file.txt', 'r', encoding='utf-8', errors='replace') as f:
    content = f.read()

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.