Python errors parameter description, use

Table of contents

Python errors parameter

Parameter value description

Example of using parameter values

strict

ignore

replace

xmlcharrefreplace

backslashreplace

namereplace


Python errors parameter

Parameter value description

In Python, when encoding, decoding, or converting between strings, the errorsparameter can be used to handle errors in the codec or conversion.

Commonly used errorsparameter values ​​are:

  • strict: An exception is thrown when a character that cannot be decoded or encoded is encountered UnicodeError.
  • 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

strictis the default errorsparameter 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

ignoreParameters 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

replaceThe 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

xmlcharrefreplaceThe 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

backslashreplaceThe 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

namereplaceThe 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, 你好!

Guess you like

Origin blog.csdn.net/songpeiying/article/details/132233255