UnicodeDecodeError:“ utf8”编解码器无法解码位置3的字节0xa3:无效的起始字节 [英] UnicodeDecodeError: 'utf8' codec can't decode byte 0xa3 in position 3: invalid start byte
问题描述
我正在使用 repl.it
Python Web IDE,但我真的无法解决问题。
I am using repl.it
Python web IDE, and I really can't solve a problem with it.
我试图解码一个字符串,但是似乎没有办法。
I was trying to decode a string, but it seems that there's no way to do it.
import base64
ciphertext = 'FxM7o1wl/7wE9CHPNzbB944feDFXbTSVaJfaLsUMzH5EP4xZRz7Sq8O3y7+jPbXIMVRxpvJZZm7ugqQ4fwpJwtvnB0/BoU+hhGeEZZZ0fFj1irm/zg3bsxOoxBJx4B3U'
ciphertext = base64.b64decode(ciphertext)
print ciphertext
UnicodeDecodeError:'utf8'编解码器无法解码位置3的字节0xa3起始字节
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa3 in position 3: invalid start byte
推荐答案
您不能 print 密文
,因为它是一个无意义的二进制字节序列,所以不是一个文本(我检查过)。
You cannot print ciphertext
, since it is a sequence of nonsensical binary bytes, not a text at all (I checked).
您的终端假设您打印某些内容,则该内容为UTF8;而则不是。因此出现错误。如果您的密文为 VGhpcyB3aWxsIGJlIHByaW50ZWQuCg ==
,那么它将毫无问题地打印出来,因为它会解码为有效的UTF-8(实际上是有效的ASCII-7)。
Your terminal is assuming that if you print something, that something is UTF8; and it is not. Hence the error. If you had a ciphertext of VGhpcyB3aWxsIGJlIHByaW50ZWQuCg==
, that would be printed without problems, since it decodes to valid UTF-8 (valid ASCII-7 actually).
如果要显示密文,可以将非UTF8字符替换为空格,也可以将密文打印为十六进制。
If you want to display the ciphertext, you can replace non-UTF8 characters with spaces, or you can print the ciphertext as hex.
但是,实际上,您真正应该做的是在打印之前对其进行解密(另外,在完成处理后,请确认它是UTF8文本,而不是用ISO-8859-15或其他字符集进行编码。是的,您可以使用适当的编解码器; 此答案还提供了有关字符集的有用信息。
But, actually, what you should really do is decrypt it before printing (also, when you've done it, verify it is a UTF8 text and not, say, encoded in ISO-8859-15 or other charsets. If it is, you can use the appropriate codec; this answer also supplies useful information on charsets).
这篇关于UnicodeDecodeError:“ utf8”编解码器无法解码位置3的字节0xa3:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!