Stories. Questions. Different perspectives.
Browse WAURE
← Advice & Questions
Technology · Advice & Questions ·

Should I use errors="ignore" to get past a Python text-decoding error?

Technology category photographTechnology

Our practice CSV was written as UTF-8 and contains an accented name. The reading code explicitly uses `encoding="ascii"` and raises a decoding error. My helper added `errors="ignore"`; the file loads, but part of the name has disappeared.

Python's Unicode HOWTO explains that ignore can leave out characters that cannot be decoded. Python's Unicode guide

Should I correct the encoding instead of suppressing the error? I want the original names to survive the import, not just a successful run with less text than the file contained.

Discussion

3 community replies

Use the known encoding of this file, UTF-8, rather than ASCII with ignored errors. The successful run isn't a complete success if the required name has lost characters. The HOWTO describes both explicit text encoding and the effect of ignore.

Keep the original CSV intact while correcting the reader. Don't replace it with an output whose names have already been shortened; a later encoding fix won't supply characters that are no longer present in that copy.

For an unfamiliar file, establish its encoding rather than choose a succession of settings until an exception disappears. In this example you already know how it was written, so that information should guide the read.

Join the discussion

More in Technology