简体   繁体   English

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

[英]Decode base64 encoded file and print results to the console

So I have a file that I have already encoded with base64, now I want to decode it back but instead of creating another file, I want to decode it in the console and print the results to screen. 因此,我有一个已经用base64编码的文件,现在我想将其解码回去,但是我不想在控制台中对其进行解码,而是将结果打印到屏幕上,而不是创建另一个文件。 How to do that? 怎么做?

encoded file string = MUhRRy1ITVRELU0zWDItNlcxSA== 编码的文件字符串= MUhRRy1ITVRELU0zWDItNlcxSA==

FYI: this would mean first opening the file in console, then decoding the give string 仅供参考:这意味着先在控制台中打开文件,然后解码给定字符串

Thanks 谢谢

Unless I'm overlooking something, this is as simple as reading in your encoded string and then calling the standard library's base64.b64decode function on it. 除非我忽略了某些内容,否则这就像读取您的编码字符串然后在其上调用标准库的base64.b64decode函数一样简单。

Something like: 就像是:

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

Using base64.decode , set sys.stdout ( sys.stdout.buffer.raw in python 3.x) as output. 使用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)

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

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