解码base64编码文件并将结果打印到控制台

时间:2013-07-30 05:18:45

标签: python base64

所以我有一个我已用base64编码的文件,现在我想要将其解码回来,但我没有创建另一个文件,而是想在控制台中对其进行解码并将结果打印到屏幕上。怎么做?

编码文件字符串= MUhRRy1ITVRELU0zWDItNlcxSA==

供参考: 这意味着首先在控制台中打开文件,然后解码给定字符串

由于

2 个答案:

答案 0 :(得分:1)

除非我忽略了某些内容,否则就像读取编码字符串然后在其上调用标准库的base64.b64decode函数一样简单。

类似的东西:

with open(path_to_encoded_file) as encoded_file:
    print base64.b64decode(encoded_file.read().strip())

答案 1 :(得分:0)

使用base64.decode,将sys.stdout(python 3.x中的sys.stdout.buffer.raw)设置为输出。

import sys
import base64

with open('filepath') as f:
    base64.decode(f, sys.stdout)