使用从Python中的字符串列表中获得的键从JSON文件中删除对象

2024-05-17 10:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json,格式如下:

{ "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" }

我试图删除基于id字段的json对象的任何重复/旧版本(即带有id=Some_ID_01id=Some_ID_02的对象被认为是重复的)。你知道吗

到目前为止,我已经成功地将json解析为python,并创建了一个需要删除的所有id的列表。实际上,我一直在使用这个列表来删除/弹出我解析的json中的对象,这样我就可以将结果重写为一个新的json文件,更不用说它远没有得到优化(我的json文件中有大约20k个对象)

到目前为止,这是我的python代码:

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循环是我最近一次尝试删除,但我得到了错误:

TypeError: unhashable type: 'dict'


Tags: the对象keyinidjsonfordata
1条回答
网友
1楼 · 发布于 2024-05-17 10:18:35

出现错误是因为^{}需要索引,而不是对象。你知道吗

然而,这有点不相关,因为it's a bad idea to modify a list that you're iterating over。你知道吗

我会考虑使用列表理解;类似于good_features = [i for i in json_data['feature'] if i['id'] not in IDList]

相关问题 更多 >