读取、修改、保存 .json文件
1.读取
with open(path, "r") as f:
json_l = json.load(f)
x = json_l["关键字1"]["关键字2"]["关键字3"]
如果打开的文件中有中文名称可使用:
with open(path, "r", encoding='ISO-8859-1') as f:
json_l = json.load(f)
x = json_l["关键字1"]["关键字2"]["关键字3"]
2.修改
json_l["关键字1"]["关键字2"]["关键字3"] = 内容
3.保存
# f是新的, x是要保存的文件,path是要保存的地址
with open(path, "w", encoding='ISO-8859-1') as f:
json.dump(f, x)
4.修改“imageData”的图片信息
# x是需要修改的, path是图片地址
with open(path, "rb", encoding='ISO-8859-1') as f:
img_data = f.read()
img_base64 = base64.b64encode(img_data).decode()
x["imageData"] = img_base64