使用Python从此JSON对象中提取嵌套列表

时间:2016-11-30 18:20:27

标签: python json

我正在使用此python脚本(尝试)从JSON对象中提取嵌套列表。

import json
from collections import defaultdict
from pprint import pprint

with open('data-science.txt') as data_file:
    data = json.load(data_file)

locations = defaultdict(int)

for item in data['included']:
    location = item['attributes']
    print(location)

我得到以下输出:

{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data science'}
{'CEO': None, 'abbreviation': None, 'logoUrl': None, 'title': 'Make IT London'}
{'name': 'Victoria', 'coord': [51.503378, -0.139134]}
{'name': 'United Kingdom', 'coord': None}
{'name': 'data mining'}
{'name': 'data analysis'}

但我真正想要的是与'coord'相关联的"id"列表。

单个记录如下所示:

    {
        "id": 3,
        "type": "location",
        "attributes": {
            "name": "Victoria",
            "coord": [
                51.503378,
                -0.139134
            ]
        }
    },

如何仅提取"id": 3"coord": [ 51.503378, -0.139134 ]

4 个答案:

答案 0 :(得分:2)

这有点勉强但可能有所帮助。基线 - 您可能希望在python中使用get函数。 (见:https://docs.python.org/2/library/stdtypes.html#dict.get

我不会在下面的代码上扩展太多 - 这很简单 - 但是你可以在它周围添加一些逻辑来检查id是否为无或者coord是None并为你自己的目的做额外的处理。

for record in data['included']:
    id = record.get('id', None)
    coord = record.get('attributes', {}).get('coord', None)

答案 1 :(得分:1)

您必须使用其键访问子结构:

coords = {}
for item in data['included']:
    coords[item['id']] = item['attributes']['coords']

答案 2 :(得分:0)

>>> data
{'id': 3, 'attributes': {'coord': [51.503378, -0.139134], 'name': 'Victoria'}, 'type': 'location'}
>>> from operator import itemgetter
>>> my_id = itemgetter('id')
>>> attributes = itemgetter('attributes')
>>> coord = itemgetter('coord')
>>> 
>>> my_id(data), coord(attributes(data))
(3, [51.503378, -0.139134])
>>> {my_id(data) : coord(attributes(data))}
{3: [51.503378, -0.139134]}
>>> d = {}
>>> d[my_id(data)] = coord(attributes(data))
>>> d
{3: [51.503378, -0.139134]}
>>> 

答案 3 :(得分:0)

我假设,idtype始终通过JSON响应提供,如果typelocation,那么coord也会被提供:

location_map = {}

for item in data.get('included', [])
    if item['type'] == 'location':
        location_map[item['id']] = item['attributes']['coord']

print location_map

或者以更加pythonic的方式:

location_map = {
    item['id']: item['attributes']['coord']
    for item in data.get('included', []) if item['type'] == 'location'
}
print location_map

样本输入:

[
  {
    "id": 3,
    "type": "location",
    "attributes": {
        "name": "Victoria",
        "coord": [
            51.503378,
            -0.139134
        ]
     }
  }
]

结果将是:

{3: [51.503378, -0.139134]}

供参考,请参阅Dict Comprehensions:https://www.python.org/dev/peps/pep-0274/