问题描述
我有一个json格式如下:
I have a json in the following format:
{
"features": [{
"geometry": {
"coordinates": [
[
[-12.345, 26.006],
[-78.56, 24.944],
[-76.44, 24.99],
[-76.456, 26.567],
[-78.345, 26.23456]
]
],
"type": "Polygon"
},
"id": "Some_ID_01",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
[139.345, 39.2345],
[139.23456, 37.3465],
[141.678, 37.7896],
[141.2345, 39.6543],
[139.7856, 39.2345]
]
],
"type": "Polygon"
},
"id": "Some_OtherID_01",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
}, {
"geometry": {
"coordinates": [
[
[143.8796, -30.243],
[143.456, -32.764],
[145.3452, -32.76],
[145.134, -30.87],
[143.123, -30.765]
]
],
"type": "Polygon"
},
"id": "Some_ID_02",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
我试图删除任何重复/旧版本的json对象基于id字段(即。
Im trying to remove any duplicates/older versions of the json object based on the id field (ie. the object with id=Some_ID_01
and id=Some_ID_02
are considered duplicates for my purposes).
到目前为止,我已经设法将json解析成python,并创建了需要删除的所有ID的列表。我被困在实际上使用该列表来删除/弹出json中的对象我解析,所以我可以将结果重写为一个新的json文件,更不用说它远未优化(我的json文件中有大约20k个对象)
So far I have manages to parse the json into python and create a list of all the IDs that require removal. I am stuck in actually using that list to delete/pop the objects from the json I parse in so I can rewrite the result to a new json file, not to mention it is far from optimized (my json file has some 20k objects in it)
这是迄今为止的python代码:
This is my python code so far:
import json
json_file = open('features.json')
json_str = json_file.read()
json_data = json.loads(json_str)
dictionaryOfJsonId = {}
removalCounter = 0
keyToRemove = []
valueToRemoveFromList = []
IDList = []
removedSometing = 0
for values in json_data['features']: #This loop converts the values in the json parse into a dict of only ID
stringToSplit = values["id"] #the id values from the json file
IDList.append(stringToSplit) #list with all the ID
newKey = stringToSplit[:-2] #takes the initial substring up to the last 2 spaces (version)
newValue = stringToSplit[-2:] #grabs the last two characters of the string
if newKey in dictionaryOfJsonId:
dictionaryOfJsonId[newKey].append(newValue)
else:
dictionaryOfJsonId[newKey] = [newValue]
for key in dictionaryOfJsonId: #Remove entries that do not have duplicates
if len(dictionaryOfJsonId[key])<2:
valueToRemoveFromList.append(str(key + dictionaryOfJsonId[key][0]))
else:
valueToRemoveFromList.append(str(key +max(dictionaryOfJsonId[key])))
for string in valueToRemoveFromList: #Remove all values that don't have duplicates from the List of ID
IDList.remove(string)
removalCounter+=1
for i in json_data['features']:
for x in IDList:
if i['id'] == x:
json_data.pop(i)
最后一个for循环是我最近尝试删除的尝试,但我收到错误:
The last for loop was my latest attempt at doing the deletion, but I get the error:
推荐答案
您收到错误,因为期望一个索引,而不是一个对象。
You're getting an error because pop
expects an index, not an object.
然而,这是自从修改您要迭代的列表是一个坏主意,这有点不相关。
我会考虑使用列表的理解;像 good_features = [i for json_data ['feature'] if i ['id'] not in IDList]
这篇关于使用从Python中的字符串列表获取的密钥从JSON文件中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!