python
encoding_error
official
true
UnicodeError
ID: python/unicodeerror
80%Fix Rate
95%Confidence
0Evidence
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
A Unicode-related encoding or decoding error occurred. This is the base class for UnicodeEncodeError, UnicodeDecodeError, and UnicodeTranslateError.
genericOfficial Documentation
https://docs.python.org/3/library/exceptions.htmlWorkarounds
-
90% success Always specify encoding explicitly: open(file, encoding='utf-8')
Never rely on locale.getpreferredencoding() which varies by OS
-
90% success Use errors='replace' or 'surrogateescape' for controlled handling of bad bytes
open(f, encoding='utf-8', errors='replace') replaces bad chars with U+FFFD
Dead Ends
Common approaches that don't work:
-
Using errors='ignore' to skip bad characters
80% fail
Silently loses data; may produce incorrect output