大家好,我尝试了以下代码,最适合从 json 文件中删除对象
#!/usr/bin/python
# Load the JSON module and use it to load your JSON file.
# I'm assuming that the JSON file contains a list of objects.
import json
obj = json.load(open("pandas.json"))
# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
for i in range(len(obj)):
#path = ["000000000036.jpg","000000000049.jpg", "000000000077.jpg"]
if obj[i]["path"] == "000000000036.jpg":
obj.pop(i)
break
# Output the updated file with pretty JSON
open("updated_file.json", "w").write(
json.dumps(obj)
)
在这里,我有一个问题,如果我想从我尝试过但失败的 json 文件中删除许多随机 json 对象,我应该怎么做,请让我清楚“帮助欣赏”。