UnicodeDecodeError: 'utf8' codec can't decode byte 0xa3 in position 3: invalid start byte

心已入冬 提交于 2019-12-13 09:39:55

问题


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' codec can't decode byte 0xa3 in position 3: invalid start byte


回答1:


You cannot print ciphertext, since it is a sequence of nonsensical binary bytes, not a text at all (I checked).

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).

If you want to display the ciphertext, you can replace non-UTF8 characters with spaces, or you can print the ciphertext as hex.

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).



来源:https://stackoverflow.com/questions/40369222/unicodedecodeerror-utf8-codec-cant-decode-byte-0xa3-in-position-3-invalid-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!