简体   繁体   English

在str.decode / encode中,replace的错误处理选项是什么意思?

[英]What's does error handling option of replace mean in str.decode/encode?

The Python documentation on str.decode and encode allow an "error handling scheme". 关于str.decode和编码的Python文档允许使用“错误处理方案”。 What does the replace option mean or do? replace选项是什么意思或做什么?

If you follow the link in the doc you referenced, it goes here: https://docs.python.org/2/library/codecs.html#codec-base-classes 如果您按照引用的文档中的链接进行操作,则会转到此处: https : //docs.python.org/2/library/codecs.html#codec-base-classes

Which says replace does this: 上面说replace做到这一点:

Replace with a suitable replacement character; 用合适的替换字符替换; Python will use the official U+FFFD REPLACEMENT CHARACTER for the built-in Unicode codecs on decoding and '?' Python将对解码和'?'使用内置的Unicode编解码器的官方U + FFFD REPLACEMENT CHARACTER on encoding. 关于编码。

U+FFFD is: U + FFFD为:

Used to replace an incoming character whose value is unknown or unrepresentable in Unicode. 用于替换值未知或无法在Unicode中表示的传入字符。

So basically the replace option puts a "dummy" character in the output wherever the input had a "bad" character that could not be decoded or encoded. 因此,基本上, replace选项将在输入中包含无法解码或编码的“不良”字符的地方,在输出中放置“虚拟”字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM