我尝试解码存储为 BLOB 的 PDF 并将其保存到扩展名为 .pdf 的文件中。 results[0][1]具有从数据库查询中提取的BLOB数据。
blob_val=results[0][1]
if len(blob_val) % 4 != 0:
while len(blob_val) % 4 != 0:
blob_val = blob_val + b"="
decod_text = base64.b64decode(blob_val)
else:
decod_text = base64.b64decode(blob_val)
尽管我在末尾添加了 = 来纠正填充错误,但它仍然显示不正确的填充错误。为什么即使我们用“=”纠正它仍然显示这个错误?
最佳答案
每个 base64 字符都编码六位。为此,字节总数应能被三整除,而不是四。
这应该可行(并且有点简化):
blob_val = results[0][1]
# If the length is divisible by 3, the 'while' will never
# be entered, so no point in doing the additional 'if' above.
while len(blob_val) % 3 != 0:
blob_val += b"="
decod_text = base64.b64decode(blob_val)
关于python - 解码 Base64 编码时填充错误错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38006798/