Skip to main content
 首页 » 编程设计

python之在嵌套的 json/dict 中搜索与指定键匹配的多个键值

2024年12月31日10mfryf

我有一个 Python 对象,它包含多层字典和列表,其中包含我需要从中获取值的键。我找到了一个 answer使用递归生成器可以提取一个键的值,但不能提取多个键的值。这是代码:

with open('data.json') as f: 
    json_data = json.load(f) 
 
def find_key(obj, key): 
    if isinstance(obj, dict): 
        yield from iter_dict(obj, key, []) 
    elif isinstance(obj, list): 
        yield from iter_list(obj, key, []) 
 
def iter_dict(d, key, indices): 
    for k, v in d.items(): 
        if k == key: 
            yield indices + [k], v 
        if isinstance(v, dict): 
            yield from iter_dict(v, key, indices + [k]) 
        elif isinstance(v, list): 
            yield from iter_list(v, key, indices + [k]) 
 
def iter_list(seq, key, indices): 
    for k, v in enumerate(seq): 
        if isinstance(v, dict): 
            yield from iter_dict(v, key, indices + [k]) 
        elif isinstance(v, list): 
            yield from iter_list(v, key, indices + [k]) 
 
 
for c in find_key(json_data, 'customer_count'): 
    print(c) 

结果:

(['calendar', 'weeks', 0, 'days', 1, 'availabilities', 0, 'customer_count'], 14) 
(['calendar', 'weeks', 0, 'days', 2, 'availabilities', 0, 'customer_count'], 7) 

另一个post有一个提取多个键的示例,但不会递归整个对象:

[...] 
keys = ("customer_count", "utc_start_at", "non_resource_bookable_capacity") 
for k in keys: 
    keypath, val = next(find_key(json_data, k)) 
    print("{!r}: {!r}".format(k, val)) 

结果:

'customer_count': 14 
'utc_start_at': '2018-09-29T16:45:00+0000' 
'non_resource_bookable_capacity': 18 

如何遍历整个对象并提取上面显示的三个键?

我想要的结果看起来像这样:

'customer_count': 14 
'utc_start_at': '2018-09-29T16:45:00+0000' 
'non_resource_bookable_capacity': 18 
 
'customer_count': 7 
'utc_start_at': '2018-09-29T16:45:00+0000' 
'non_resource_bookable_capacity': 25 

sample json

请您参考如下方法:

下面的示例函数在字典(包括所有嵌套字典)中搜索与您要查找的键列表匹配的键/值对。此函数递归循环字典和任何嵌套字典并列出它包含的列表,以构建要检查匹配键的所有可能字典的列表。

def find_key_value_pairs(q, keys, dicts=None): 
    if not dicts: 
        dicts = [q] 
        q = [q]   
 
    data = q.pop(0) 
    if isinstance(data, dict): 
        data = data.values() 
 
    for d in data: 
        dtype = type(d) 
        if dtype is dict or dtype is list: 
            q.append(d) 
            if dtype is dict: 
                dicts.append(d) 
 
    if q: 
        return find_key_value_pairs(q, keys, dicts) 
 
    return [(k, v) for d in dicts for k, v in d.items() if k in keys] 

下面的示例使用 json.loads 将类似于您的 json 的示例数据集转换为字典,然后再将其传递给函数。

import json 
 
json_data = """ 
{"results_count": 2, "results": [{"utc_start_at": "2018-09-29T16:45:00+0000", "counts": {"customer_count": "14", "other_count": "41"}, "capacity": {"non-resource": {"non_resource_bookable_capacity": "18", "other_non_resource_capacity": "1"}, "resource_capacity": "10"}}, {"utc_start_at": "2018-10-29T15:15:00+0000", "counts": {"customer_count": "7", "other_count": "41"}, "capacity": {"non-resource": {"non_resource_bookable_capacity": "25", "other_non_resource_capacity": "1"}, "resource_capacity": "10"}}]} 
""" 
data = json.loads(json_data) # json_data is a placeholder for your json 
keys = ['results_count', 'customer_count', 'utc_start_at', 'non_resource_bookable_capacity'] 
results = find_key_value_pairs(data, keys) 
for k, v in results: 
    print(f'{k}: {v}') 
# results_count: 2 
# utc_start_at: 2018-09-29T16:45:00+0000 
# utc_start_at: 2018-10-29T15:15:00+0000 
# customer_count: 14 
# customer_count: 7 
# non_resource_bookable_capacity: 18 
# non_resource_bookable_capacity: 25