简体   繁体   English

Base64 将文本文件解码为 Python 中的 Bin 文件

[英]Base64 Decode Text File To Bin File In Python

I have a text file and I want to decode it to binary code file.我有一个文本文件,我想将其解码为二进制代码文件。

Currently, I'm using openssl on Windows to do this.目前,我在openssl上使用 openssl 来执行此操作。 The command is: base64 -d input.txt output.bin命令为: base64 -d input.txt output.bin

What could be the equivalent Python's base64 command for this?什么可能是等效的 Python 的base64命令?

Edit:编辑:

input.txt Content: input.txt内容:

India
USA
Africa
Europe

My try:我的尝试:

InputFile = open('input.txt', 'r')
InputFile_Content = InputFile.read()
print(InputFile_Content)

print(base64.decode(InputFile_Content, 'Output.bin'))

Error:错误:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print(base64.decode(InputFile_Content, 'Output.bin'))
  File "Python\Python37\lib\base64.py", line 502, in decode
    line = input.readline()
AttributeError: 'str' object has no attribute 'readline'

A quick Google search brought up this article about encoding and decoding base64 strings.一个快速的谷歌搜索带来了这篇关于编码和解码 base64 字符串的文章。 Probably a good way to do that in python is to do:在 python 中这样做的一个好方法可能是:

import base64

with open("input.txt", "r") as f:
    message = f.read().encode("ascii")

with open("output.bin", "wb") as f:
    f.write(base64.b64encode(message))

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

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