二进制解码为pd

2024-04-26 22:24:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是一个平台,当你上传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'))

编辑:

code = "My binary string"

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

数据库中的二进制字符串示例:“65/658e9014babd33786821f3130c5f3a1cc1322df” 所以我假设它是在“/”符号之后开始的?


Tags: 字符串test数据库stringpdfmywith二进制
1条回答
网友
1楼 · 发布于 2024-04-26 22:24:27

base64.decode(,)将文件作为参数。你想试试

fout.write(base64.decodestring(code))

尽管您的代码示例没有编码。 下面是一个工作示例:

#!/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))

相关问题 更多 >