[英]Extract a nested list from this JSON object with Python
I'm using this python script ( in an attempt ) to extract a nested list from a JSON object. 我正在使用这个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)
I get the following output: 我得到以下输出:
{'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'}
But really what I want is the 'coord'
list associated with an "id"
. 但我真正想要的是与"id"
相关联的'coord'
列表。
A single record looks like this: 单个记录如下所示:
{
"id": 3,
"type": "location",
"attributes": {
"name": "Victoria",
"coord": [
51.503378,
-0.139134
]
}
},
How can I extract the only the "id": 3
and "coord": [ 51.503378, -0.139134 ]
? 我怎样才能提取出唯一的"id": 3
和"coord": [ 51.503378, -0.139134 ]
?
This is a little bare-bones but may help. 这有点勉强但可能有所帮助。 Baseline - you might want to use the get
function in python. 基线 - 你可能想在python中使用get
函数。 (See this: https://docs.python.org/2/library/stdtypes.html#dict.get ) (见: https : //docs.python.org/2/library/stdtypes.html#dict.get )
I won't expand too much on the below code - it is fairly simple - but you can add some logic around it to check if id is None or if coord is None and do additional processing for your own purposes. 我不会在下面的代码上扩展太多 - 它相当简单 - 但是你可以在它周围添加一些逻辑来检查id是否为None或者coord是None并且为了你自己的目的进行额外的处理。
for record in data['included']:
id = record.get('id', None)
coord = record.get('attributes', {}).get('coord', None)
You have to access the sub-structure with its key: 您必须使用其键访问子结构:
coords = {}
for item in data['included']:
coords[item['id']] = item['attributes']['coords']
>>> 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]}
>>>
I am assuming, the id
and type
are always provided through JSON response, and if type
is location
then coord
will be given too: 我假设, id
和type
总是通过JSON响应提供,如果type
是location
那么coord
也将被给出:
location_map = {}
for item in data.get('included', [])
if item['type'] == 'location':
location_map[item['id']] = item['attributes']['coord']
print location_map
OR in more pythonic way: 或者以更加pythonic的方式:
location_map = {
item['id']: item['attributes']['coord']
for item in data.get('included', []) if item['type'] == 'location'
}
print location_map
For sample input: 对于样本输入:
[
{
"id": 3,
"type": "location",
"attributes": {
"name": "Victoria",
"coord": [
51.503378,
-0.139134
]
}
}
]
result would be: 结果将是:
{3: [51.503378, -0.139134]}
For reference see Dict Comprehensions: https://www.python.org/dev/peps/pep-0274/ 供参考,请参阅Dict理解: https : //www.python.org/dev/peps/pep-0274/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.