小编典典

如何从python中的字典中选择深度嵌套的key:values

python

我已经从网站下载了json数据,我想从嵌套的json中选择特定的key:values。我将json转换为python字典。然后,我使用字典理解来选择嵌套的key:values,但是嵌套太多了,我相信有比单独扩展每个字典更好的方法。我在我的方法中看到了冗余。您能建议一个更好的方法吗?

{
    "success": true,
    "payload": {
        "tag": {
            "slug": "python",
            "name": "Python",
            "postCount": 10590,
            "virtuals": {
                "isFollowing": false
            }
        },
        "metadata": {
            "followerCount": 18053,
            "postCount": 10590,
            "coverImage": {
                "id": "1*O3-jbieSsxcQFkrTLp-1zw.gif",
                "originalWidth": 550,
                "originalHeight": 300
            }
        }
    }
}

我的方法:

从datetime导入datetime,timedelta

import json,re

data=r'data.json'
#reads json and converts to dictionary
def js_r(data):
    with open(data, encoding='Latin-1') as f_in:
        return json.load(f_in)

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])
if __name__=="__main__":
    my_dict=js_r(data)
    print ( "This is dictionary for python tag",my_dict)
    keys=my_dict.keys()
    print ("This is the dictionary keys",my_dict.keys())
    my_payload=list(find_key(my_dict,'title'))
    print ("These are my payload",my_payload)
    my_post=iter_dict(my_dict,'User','id')
    print(list(my_post))

阅读 290

收藏
2021-01-20

共1个答案

小编典典

我建议您使用python-benedict,具有完整 keypath支持 和许多实用程序方法的可靠python dict子类。

它提供多种格式的IO支持,包括json

您可以直接从json文件初始化它:

from benedict import benedict

d = benedict.from_json('data.json')

现在,您的字典具有关键路径支持:

print(d['payload.metadata.coverImage.id'])

# or use get to avoid a possible KeyError
print(d.get('payload.metadata.coverImage.id'))

安装: pip install python-benedict

这里是库存储库和文档:https :
//github.com/fabiocaccamo/python-
benedict

注意:我是这个专案的作者

2021-01-20