python
encoding_error
ai_generated
true
UnicodeDecodeError: 'utf-8' codec can't decode byte
ID: python/unicodedecodeerror
82%Fix Rate
85%Confidence
210Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
File contains non-UTF-8 bytes. Common with legacy data, binary files, or Windows-generated CSVs.
genericWorkarounds
-
88% success Detect encoding with chardet/charset-normalizer then open with correct encoding
import charset_normalizer; detected = charset_normalizer.from_path(path).best()
-
80% success Open with errors='replace' or errors='ignore' when data loss is acceptable
with open('data.csv', encoding='utf-8', errors='replace') as f: content = f.read() # Replaces undecodable bytes with U+FFFD # Or use errors='ignore' to silently drop bad bytesSources: https://docs.python.org/3/library/functions.html#open
Dead Ends
Common approaches that don't work:
-
Force encoding='utf-8' everywhere
75% fail
File genuinely isn't UTF-8, forcing it corrupts or crashes
-
Strip non-ASCII bytes
68% fail
Loses legitimate non-ASCII data like names, currencies
Error Chain
Preceded by: