base64 解码输出具有非 ascii 字符

base64 decode output has non-ascii characters

我无法正确解码 base64 数据。它正确解码消息,但也包含大量非 ascii 字符,然后我也必须清理这些字符,所以我想知道我是否解码不正确,或者我是否需要创建一个脚本来清理文本 post解码。下面是 python 代码和我要说明的部分输出。谢谢!

message= base64.b64decode(base64_message).decode(errors='ignore')

您显然是在尝试解码 Word 文档,根据定义,该文档根本不是纯文本。确保您要解码的是文本。否则将解码结果保存到文件(file.docx?)并在适当的应用程序中打开它。


在评论中跟进您的问题,您不必从 base64 获取文本,保持原样并写入文件。而不是

base64.b64decode(base64_message).decode(errors='ignore')

只使用

base64.b64decode(base64_message)

一切都会好的:

>>> a = base64.b64encode('\x01\x02\x04')
>>> a
'AQIE'
>>> base64.b64decode(a)
'\x01\x02\x04'