Python Glossary Русский

all Python 3codecsfunction

codecs.ignore_errors()

error handlers

The 'ignore' error handler: drops characters and bytes that could not be encoded or decoded, and carries on.

Syntax

codecs.ignore_errors(exception)

The most dangerous handler: data disappears without a trace, and the result gives no hint that anything went missing. Read a cp1251 file as utf-8 with errors='ignore' and all the Cyrillic vanishes while the program 'succeeds'. For diagnosis prefer 'replace' or 'backslashreplace', which at least leave a mark.

Examples

data = 'Привет, world'.encode('cp1251')
print(data.decode('utf-8', 'ignore'))  # → , world
print(data.decode('cp1251'))  # → Привет, world
import codecs
try:
    'é'.encode('ascii')
except UnicodeEncodeError as err:
    print(codecs.ignore_errors(err))  # → ('', 1)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy