44
I'm getting UnicodeDecodeError when reading files or processing text. How do I properly handle encoding in Python?
Loading...
Q&A · Answered
I'm getting UnicodeDecodeError when reading files or processing text. How do I properly handle encoding in Python?
Accepted answer
Unicode errors are common when dealing with files from different sources. Here's how to handle them:
# 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()
# 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()Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.