gpt4 book ai didi

python - 在不明确调用键名的情况下解析 json

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:56 25 4
gpt4 key购买 nike

我有一个类似 json 的文件,每行都有有效的 json,看起来像

{"Some_key": {"name": "Tom", "school_code":"5678", "sport": "football", "score":"3.46", "classId": "456"}}
{"Another_one": {"name": "Helen", "school_code":"7657", "sport": ["swimming", "score":"9.8", "classId": "865"}}
{"Yet_another_one_": {"name": "Mikle", "school_code":"7655", "sport": "tennis", "score":"5.7", "classId": "76532"}}

所以我需要通过提取这些第一个键(即“Some_Key”、“Another_key”等),我事先不知道,然后将字典中分数键的值与它们相关联。所以像这样:

("Some_key":"3.46", "Another_Key":"9.8", "Yet_another_one_"; "5.7")

我不知道在不显式调用键名的情况下提取内容的方法,所以非常欢迎您的想法!

最佳答案

这适用于 Python2 和 Python3

>>> {k: v.get('score') for item in data for k, v in item.items()}
{'Another_one': '9.8', 'Some_key': '3.46', 'Yet_another_one_': '5.7'}

关于python - 在不明确调用键名的情况下解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com