python encoding_error ai_generated true

UnicodeDecodeError: 'utf-8' codec can't decode byte

ID: python/unicodedecodeerror

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
210Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

File contains non-UTF-8 bytes. Common with legacy data, binary files, or Windows-generated CSVs.

generic

Workarounds

  1. 88% success Detect encoding with chardet/charset-normalizer then open with correct encoding
    import charset_normalizer; detected = charset_normalizer.from_path(path).best()

    Sources: https://docs.python.org/3/library/codecs.html

  2. 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 bytes

    Sources: https://docs.python.org/3/library/functions.html#open

Dead Ends

Common approaches that don't work:

  1. Force encoding='utf-8' everywhere 75% fail

    File genuinely isn't UTF-8, forcing it corrupts or crashes

  2. Strip non-ASCII bytes 68% fail

    Loses legitimate non-ASCII data like names, currencies

Error Chain

Leads to:
Preceded by: