简体   繁体   English

base64 解码输出具有非 ascii 字符

[英]base64 decode output has non-ascii characters

I am having trouble properly decoding base64 data.我无法正确解码 base64 数据。 It decodes the message properly, but also includes a ton of non-ascii characters which then I have to clean as well, so I was wondering if I was decoding it incorrectly or if I will need to create a script to clean the text post decoding.它正确解码消息,但还包含大量非 ascii 字符,然后我也必须清理这些字符,所以我想知道我是否错误地解码了它,或者我是否需要创建一个脚本来清理解码后的文本. Below is the python code and part of the output I am getting to illustrate.下面是 python 代码和我要说明的部分输出。 Thanks!谢谢!

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

在此处输入图片说明

You're obviously trying to decode a Word document, which is by definition not plain text at all.您显然是在尝试解码Word文档,根据定义,该文档根本不是纯文本。 Make sure what you're trying to decode is text.确保您要解码的是文本。 Otherwise save the decoding result to a file ( file.docx ?) and open it in the appropriate application.否则将解码结果保存到一个文件( file.docx ?)并在适当的应用程序中打开它。


Following up your question in the comments, you don't have to get the text from base64, leave it as it is and write to the file.在评论中跟进您的问题,您不必从 base64 获取文本,保持原样并写入文件。 Instead of代替

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

use just只用

base64.b64decode(base64_message)

and everything will be fine:一切都会好的:

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

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

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