繁体   English   中英

在复杂的嵌套字典/列表中搜索键、值对

[英]Search for key, value pairs in complex nested dictionary/list

我有一个对 API 请求的响应的列表。 结果是 JSON 有很多嵌套字典和这样的列表:

{'Siri': {'ServiceDelivery': {'ResponseTimestamp': '2020-12-13T08:16:31Z',
   'ProducerRef': 'IVTR_HET',
   'ResponseMessageIdentifier': 'IVTR_HET:ResponseMessage::df80db10-3cd3-4d8d-9cee-27ec6c716630:LOC:',
   'StopMonitoringDelivery': [{'ResponseTimestamp': '2020-12-13T08:16:31Z',
     'Version': '2.0',
     'Status': 'true',
     'MonitoredStopVisit': [{'RecordedAtTime': '2020-12-13T08:16:27.623Z',
       'ItemIdentifier': 'RATP:Item::STIF.StopPoint.Q.40944.A.7:LOC',
       'MonitoringRef': {'value': 'STIF:StopPoint:Q:40944:'},
       'MonitoredVehicleJourney': {'lineRef': {'value': 'STIF:Line::C01742:'},
        'framedVehicleJourneyRef': {'dataFrameRef': {'value': 'any'},
         'datedVehicleJourneyRef': 'RATP:VehicleJourney::RA.A.0916.1:LOC'},
        'journeyPatternName': {'value': 'TEDI'},
        'vehicleMode': [],
        'publishedLineName': [{'value': 'St-Germain-en-Laye. Poissy. Cergy. / Boissy-St-Léger. Marne-la-Vallée.'}],
        'directionName': [{'value': 'St-Germain-en-Laye. Poissy. Cergy.'}],
        'serviceFeatureRef': [],
        'vehicleFeatureRef': [],
        'originName': [],
        'originShortName': [],
        'destinationDisplayAtOrigin': [],
        'via': [],
        'destinationRef': {'value': 'STIF:StopPoint:Q:411359:'},
        'destinationName': [{'value': 'Poissy'}],
        'destinationShortName': [],
        'originDisplayAtDestination': [],
        'vehicleJourneyName': [],
        'journeyNote': [{'value': 'TEDI'}],
        'facilityConditionElement': [],
        'situationRef': [],
        'monitored': True,
        'monitoringError': [],
        'progressStatus': [],
        'trainBlockPart': [],
        'additionalVehicleJourneyRef': [],
        'monitoredCall': {'stopPointRef': {'value': 'STIF:StopPoint:Q:40944:'},
         'stopPointName': [{'value': 'Torcy'}],
         'vehicleAtStop': True,
         'originDisplay': [],
         'destinationDisplay': [],
         'callNote': [],
         'facilityConditionElement': [],
         'situationRef': [],
         'arrivalOperatorRefs': [],
         'expectedDepartureTime': '2020-12-13T08:16:00Z',
         'departurePlatformName': {'value': '2'},
         'departureOperatorRefs': []}}},

我需要一种有效的方法来提取对应于例如位于嵌套字典迷宫中某处的expectedDepartureTime键的值对。 它必须高效,因为来自源站点的格式不是很稳定,因此不同的车站(它是运输数据)对于相同的值会有不同的键,例如ExpectedDepartureTimeexpecteddepartureTime ,我可能需要尝试很多不同的东西。

PS:我在这里尝试了@hexerei 软件的解决方案, 在嵌套字典和列表中查找所有出现的键,但它只是给了我一个

<generator object gen_dict_extract at 0x00000200632C8948>

作为 output。 我如何从中获得价值?

这个 function 应该返回一个包含键值对的列表,无论深度如何:

def accumulate_keys(dct): # returns all the key-value pairs
    key_list = []
    def accumulate_keys_recursive(dct): # will accumulate key-value pairs in key_list
        for key in dct.keys():
            if isinstance(dct[key], dict):
                accumulate_keys_recursive(dct[key])
            else:
                key_list.append((key, dct[key]))
    accumulate_keys_recursive(dct)
    return key_list
print(accumulate_keys(dct))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM