Python 如何从一个大json文件中删除多个json对象
大家好,我试过下面的代码,最适合从json文件中删除对象Python 如何从一个大json文件中删除多个json对象,python,arrays,json,coco,Python,Arrays,Json,Coco,大家好,我试过下面的代码,最适合从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
#!/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对象,我应该怎么做?请让我在上面明确“帮助欣赏”。假设json数据是一个json元素数组,下面的代码将从中删除3个随机元素。根据您的需要定制
import json
import random
data = json.load(open("filepath.json"))
print(json.dumps(data, indent=4))
keys_to_be_deleted = [random.randint(0, len(data)-1) for _ in range(3)]
[data.pop(i) for i in keys_to_be_deleted]
print(json.dumps(data, indent=4))
我找到了如何从大json文件中删除特定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("new_pandas.json"))
# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
num = 0
n = 0
flag = False
for i in range(len(obj)):
path = ["000000170035.jpg","000000171962.jpg","000000004972.jpg"]
for j in path:
if obj[n]["path"] == j:
print(obj[n]["path"])
obj.pop(n)
flag = True
break
if flag == False: # Cannot find pass forward
n = n + 1
else:
flag = False
if obj[i]["path"] == "000000581357.jpg": #end object of json file
break
# print(obj[i]["path"])
num = num + 1
if num >= 100000:
break
# Output the updated file with pretty JSON
open("updated-file.json", "w").write(
json.dumps(obj)
)
非常感谢@Pradip分享一段非常好的代码,如果我想从json中删除一些特定的对象,我可以在我发布的代码中特别提到“path”值(在每个对象的开头),它会删除整个特定的对象。现在我想用“path”值删除很多对象,我有一个“path”值列表,我想从我的json中删除1000多个值。所以你有一个名为path的列表,它有1000多个值。现在,您想删除任何json元素(在您的json数组中),其中包含一个名为path的键,其值在1000+个值中。我的理解正确吗?正是Pradip先生你理解得很好。答案2有一个示例代码
#!/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("new_pandas.json"))
# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
num = 0
n = 0
flag = False
for i in range(len(obj)):
path = ["000000170035.jpg","000000171962.jpg","000000004972.jpg"]
for j in path:
if obj[n]["path"] == j:
print(obj[n]["path"])
obj.pop(n)
flag = True
break
if flag == False: # Cannot find pass forward
n = n + 1
else:
flag = False
if obj[i]["path"] == "000000581357.jpg": #end object of json file
break
# print(obj[i]["path"])
num = num + 1
if num >= 100000:
break
# Output the updated file with pretty JSON
open("updated-file.json", "w").write(
json.dumps(obj)
)