Table of contents
Example of using parameter values
Python errors parameter
Parameter value description
In Python, when encoding, decoding, or converting between strings, the errors
parameter can be used to handle errors in the codec or conversion.
Commonly used errors
parameter values are:
strict
: An exception is thrown when a character that cannot be decoded or encoded is encounteredUnicodeError
.ignore
: Ignore characters that cannot be decoded or encoded, and skip directly.replace
: Replace characters that cannot be decoded or encoded with the specified string (usually question marks or spaces).xmlcharrefreplace
: Replace characters that cannot be decoded or encoded with an XML character reference.backslashreplace
: Replace characters that cannot be decoded or encoded with backslash encoding.namereplace
: Replace characters that cannot be decoded or encoded with the Unicode name encoding.
Example of using parameter values
Here are examples using different parameter values:
strict
strict
is the default errors
parameter value of Python, and an exception will be thrown when a character that cannot be coded, decoded or converted is encountered UnicodeError
. For example:
text = "Hello, 你好!"
# 如果下面一行代码的参数改为 'ignore',则不会抛出异常
bytes = text.encode("ascii", errors="strict")
Output result:
Traceback (most recent call last):
File "<input>", line 3, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)
ignore
ignore
Parameters ignore characters that cannot be encoded, decoded or converted, and are skipped directly. For example:
text = "Hello, 你好!"
# 忽略无法编解码的字符
bytes = text.encode("ascii", errors="ignore")
print(bytes.decode("ascii", errors="ignore"))
Output result:
Hello, !
replace
replace
The parameter replaces characters that cannot be encoded or converted with the specified string (usually question marks or spaces). For example:
text = "Hello, 你好!"
# 将无法编解码的字符替换成问号
bytes = text.encode("ascii", errors="replace")
print(bytes.decode("ascii", errors="replace"))
Output result:
Hello, ??!
xmlcharrefreplace
xmlcharrefreplace
The parameter will replace characters that cannot be coded or converted with XML character reference. For example:
text = "Hello, 你好!"
# 将无法编解码的字符替换成 XML character reference
bytes = text.encode("ascii", errors="xmlcharrefreplace")
print(bytes.decode("ascii", errors="xmlcharrefreplace"))
Output result:
Hello, 你好!
backslashreplace
backslashreplace
The parameter replaces characters that cannot be coded, decoded or converted with backslash encoding. For example:
text = "Hello, 你好!"
# 将无法编解码的字符替换成反斜杠编码
bytes = text.encode("ascii", errors="backslashreplace")
print(bytes.decode("ascii", errors="backslashreplace"))
Output result:
Hello, \u4f60\u597d\uff01
namereplace
namereplace
The parameter replaces characters that cannot be coded or converted with the Unicode name encoding. For example:
text = "Hello, 你好!"
# 将无法编解码的字符替换成 Unicode 名称编码
bytes = text.encode("ascii", errors="namereplace")
print(bytes.decode("ascii", errors="namereplace"))
Output result:
Hello, 你好!