2 回答
TA贡献1828条经验 获得超3个赞
你可以试试
json_ls = [
{
"domain": "abc.com",
"action": "no action",
"date": "2020-05-15",
"status": "new"
},
{
"domain": "xyz.net",
"action": "pending",
"date": "2020-05-15",
"status": "on-hold"
}
]
for i in json_ls:
if "abc.com" in i.values():
i['action'] = "New value"
新的 json_ls
[{'domain': 'abc.com', 'action': 'New value', 'date': '2020-05-15', 'status': 'new'}, {'domain': 'xyz.net', 'action': 'pending', 'date': '2020-05-15', 'status': 'on-hold'}]
此代码将更改字典中操作的值,因为“abc.com”是 JSON/字典对象的值之一。
TA贡献1851条经验 获得超5个赞
那奏效了。我添加了一条写语句来更新文件:
import json
fhand = open("data.json")
data = json.loads(fhand.read())
for i in data:
if "xyz.net" in i.values():
i['action'] = "Takedown"
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
添加回答
举报