将二进制解码为pdf

Ran*_*son 0 python base64

我正在使用一个平台,当你将pdf上传到它时,用Python中的base64编码转换pdf.然后它将二进制字符串存储在数据库中.

现在我想解码字符串并将它们写入本地文件夹,所以我想使用'with open'结构并将b参数传递给二进制文件然后它应该根据我的解码字符串创建test.pdf并写入它到我的桌面?然而,这没有结果,我在这里做错了什么?

code = "My binary string"

with open('test.pdf', 'wb') as fout:
     fout.write(base64.decode(code, '~/Desktop'))
Run Code Online (Sandbox Code Playgroud)

编辑:

code = "My binary string"

with open('~/Desktop/test.pdf', 'wb') as fout:
     fout.write(base64.decodestring(code))
Run Code Online (Sandbox Code Playgroud)

数据库中的二进制字符串示例:"65/658e9014babd33786821f3130c5f3a1cc1322ddf"所以我假设它在'/'标记后面开始?

meu*_*euh 10

base64.decode(,)将文件作为args.你想尝试

fout.write(base64.decodestring(code))
Run Code Online (Sandbox Code Playgroud)

虽然您的代码示例未编码.这是一个有效的例子:

#!/usr/bin/python
import base64, os
code = 'TXkgYmluYXJ5IHN0cmluZw==\n'
with open(os.path.expanduser('~/Desktop/test.pdf'), 'wb') as fout:
     fout.write(base64.decodestring(code))
Run Code Online (Sandbox Code Playgroud)