Results.

SCEPTRA.TECH

Python UnicodeDecodeError: Why errors=ignore Can Quietly Change Your Import

Strict decoding stops, ignore omits malformed bytes, replace inserts a marker

A Python import fails with UnicodeDecodeError. Adding errors='ignore' makes it run, but a customer name loses a letter. That is not a repaired import. It is a different decoded result with some input omitted. I would keep the original file and check the encoding assumption before choosing an error policy.

Separate bytes, encoding and error handling

A text file contains bytes. Decoding interprets those bytes using an encoding such as UTF-8. The errors argument controls what happens when the bytes cannot be decoded under that encoding's rules. Changing the error handler does not identify the correct encoding.

Here is a small byte sequence, deliberately used as a local example rather than taken from a real customer file:

raw = b'caf\xe9,42\n'
text = raw.decode('utf-8', errors='strict')
# Raises UnicodeDecodeError

Under UTF-8, that byte sequence is invalid. If the producer confirms this sample was encoded as Windows-1252, decoding with cp1252 gives the intended accented word. That conclusion depends on knowing the producer's encoding, not on finding a codec that happens to accept the bytes.

What the easy error handlers actually do

print(ascii(raw.decode('utf-8', errors='ignore')))
# 'caf,42\n'

print(ascii(raw.decode('utf-8', errors='replace')))
# 'caf\ufffd,42\n'

print(ascii(raw.decode('utf-8', errors='backslashreplace')))
# 'caf\\xe9,42\n'

Ignore drops the malformed input. Replace inserts a replacement character during decoding. Backslashreplace makes the problematic byte visible as an escape in the decoded text. These are different output policies, not equivalent ways to recover the intended letter.

Replacement or escaped text can be useful for a diagnostic preview when clearly labeled. It should not quietly become an authoritative identifier, address or financial field. Even a visible replacement marker means the text was not decoded cleanly under the chosen rules.

A cautious file-reading path

Start with the export settings, documented file format or the system that produced the file. If it promises UTF-8, read explicitly as UTF-8 and let strict decoding expose a mismatch:

from pathlib import Path

path = Path('export.csv')
with path.open('r', encoding='utf-8', errors='strict') as stream:
    text = stream.read()

This example reads the whole file into memory and does not overwrite it. For a very large file, process it incrementally with an appropriate text reader instead. Decoding successfully still does not prove that CSV columns, dates or values satisfy your import contract.

If decoding fails, preserve the source and pause the affected import. Record the selected encoding and a safe diagnostic, then ask the producer to confirm the export encoding or regenerate the file. Do not upload sensitive files to a random online encoding checker.

A successful decode is evidence, not a guarantee

Trying encodings until one stops raising an exception can produce readable-looking but wrong text. Check known sample values with the producer, especially accented characters and punctuation, rather than treating the absence of an exception as proof. Keep any conversion output separate from the original until the result is validated.

If the file is actually binary or compressed, a text error handler is the wrong layer to fix. Confirm what the file is before interpreting arbitrary bytes as a text export.

What I checked locally

I ran the sample through Python's strict, ignore, replace and backslashreplace UTF-8 handlers, then decoded the same bytes as cp1252. The strict call failed, the other handlers produced the distinct results shown, and the original byte sequence stayed unchanged. This is a small behavior demonstration, not an encoding detector or a test of your file.

The practical rule is to choose the encoding from evidence and choose the error policy from the data's stakes. Getting past an exception is not the same as preserving the information.

Sources

Comments