Ama*_*nda 1 python parsing json 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"}}
Run Code Online (Sandbox Code Playgroud)
所以我需要通过提取这些第一个键(即"Some_Key","Another_key"等)来创建字典(?不是真的需要字典格式,而是任何可以帮助我将键与值,两个元素的数组相关联)的东西,我事先不知道,然后将它们与词典中的得分键值相关联.像这样的东西:
("Some_key":"3.46", "Another_Key":"9.8", "Yet_another_one_"; "5.7")
Run Code Online (Sandbox Code Playgroud)
我不知道在没有明确调用键名的情况下提取内容的方法,所以您的想法非常受欢迎!
这适用于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'}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |