How to handle Unicode encoding errors in Python
Answered
44
I'm getting UnicodeDecodeError when reading files or processing text. How do I properly handle encoding in Python?
O
Asked by
omar_linux
Platinum
•
593 rep
1 Answer
8
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()
S
Bronze
•
259 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer