【发布时间】:2021-01-31 22:25:22
【问题描述】:
尝试将输出转换为 Json 格式,但出现错误。删除 json.dump 后,将数据转换为 base64 格式。但是当使用 json.dump 时会显示错误。
代码:
import json
import base64
with open(r"C:/Users/Documents/pdf2txt/outputImage.jpg","rb") as img:
image = base64.b64encode(img.read())
data['ProcessedImage'] = image
print(json.dump(data)
输出:
TypeError: Object of type 'bytes' is not JSON serializable
使用时:
print(json.dumps(dict(data)))
它也显示相同的错误
【问题讨论】:
-
确保发布导致上述行为的有效代码。显示的代码将无法解析/运行,至少有两个不同的原因。如果没有重现所描述问题的有效代码,一个有时正确的假设是实际有问题的代码和显示的代码不同。
-
您遇到了同样的错误,因为它与您在
print函数调用中使用的内容无关——它来自image = base64.b64encode(img.read())行。
标签: python json python-3.x serialization base64