字典内容写入json时,需要用json.dumps将字典转换为字符串,然后再写入。
json也支持格式,通过参数indent可以设置缩进,如果不设置的话,则保存下来会是一行
import json
path = "F:\\wind data\\11.json"
with open(path, 'r') as f_five:
json_data = json.load(f_five)
for k in list(json_data.keys()): # 为了方便在遍历字典时删除数据
if k == '002':
print("下载成功")
json_data.pop(k)
print(json_data)
json_data = json.dumps(json_data)
with open('F:\\wind data\\fail.json', 'a') as f_six:
f_six.write(json_data)
结果得到:
import json
path = "F:\\wind data\\11.json"
with open(path, 'r') as f_five:
json_data = json.load(f_five)
for k in list(json_data.keys()): # 为了方便在遍历字典时删除数据
if k == '002':
print("下载成功")
json_data.pop(k)
json_data = json.dumps(json_data,indent=4)
with open('F:\\wind data\\fail.json', 'a') as f_six:
f_six.write(json_data)
结果得到: