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

标签 python json parsing dictionary key

我有一个类似 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/

相关文章:

python - reshape 我的 pandas 数据框

python - 如何复制估算器以便在多个数据集上使用它?

javascript - AmCharts 标记颜色为负时

javascript - JSON 对象作为键值对中的键

sql - 如何查询多行并解析为json?

时间:2019-03-08 标签:c#htmlagilitypack

c# - 尽可能长地匹配固定列的行

python - 试图用 praw.Reddit 抓取 Reddit

python - 如何执行两个列表的元素乘法?

PHP:在没有 {} 的情况下生成 json?