将二进制解码为pdf
Decoding binary to pdf
我正在使用一个平台,当您将 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/658e9014babd33786821f3130c5f3a1cc1322ddf”
所以我假设它在“/”标记之后开始?
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))
我正在使用一个平台,当您将 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/658e9014babd33786821f3130c5f3a1cc1322ddf” 所以我假设它在“/”标记之后开始?
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))