Python 在字典列表中查找项
在以下数据结构中:Python 在字典列表中查找项,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,在以下数据结构中: [ { "id": 28, "country": "Brazil", "country_code": "BR", "country_population": 201103330, "province": "", "last_updated": "2020-04-03T01:40:00.724616Z", "coordinates": { "latitude": "-14.235", "long
[
{
"id": 28,
"country": "Brazil",
"country_code": "BR",
"country_population": 201103330,
"province": "",
"last_updated": "2020-04-03T01:40:00.724616Z",
"coordinates": {
"latitude": "-14.235",
"longitude": "-51.9253"
},
"latest": {
"confirmed": 8044,
"deaths": 324,
"recovered": 0
},
"timelines": {
"confirmed": {
"latest": 8044,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"deaths": {
"latest": 324,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"recovered": {
"latest": 0,
"timeline": {}
}
}
}
]
如何从
“timeline”
键中获取“timeline”
项目?您应该至少提供一段您目前尝试的代码
d=[
{
“id”:28,
“国家”:“巴西”,
“国家/地区代码”:“BR”,
“国家人口”:201103330,
“省”:“,
“最后更新”:“2020-04-03T01:40:00.724616Z”,
“坐标”:{
“纬度”:“-14.235”,
“经度”:“-51.9253”
},
“最新”:{
“已确认”:8044,
“死亡”:324人,
“已恢复”:0
},
“时间表”:{
“确认”:{
“最新”:8044,
“时间线”:{
“2020-01-22T00:00:00Z”:0,
“2020-01-23T00:00:00Z”:0,
“2020-01-24T00:00:00Z”:0,
}
},
“死亡”:{
“最新”:324,
“时间线”:{
“2020-01-22T00:00:00Z”:0,
“2020-01-23T00:00:00Z”:0,
“2020-01-24T00:00:00Z”:0,
}
},
“已恢复”:{
“最新”:0,
“时间线”:{}
}
}
}
]
打印(d[0][“时间线”][“已确认”][“时间线”])
顺便说一下:
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
对我来说看起来很奇怪
时间线
是否应该是数组
而不是对象
?您的JSON确实存在问题
print([[i, data[0]['timelines'][i]['timeline']] for i in data[0]['timelines']])
“JSONDecodeError:应使用双引号括起属性名:第24行第9列(字符558)”
这又是你的时间表,如上面所示
“时间线”:{
“2020-01-22T00:00:00Z”:0,
“2020-01-23T00:00:00Z”:0,从该包装中提取的数据结构->[
import json
x = """[{
"id": 28,
"country": "Brazil",
"country_code": "BR",
"country_population": 201103330,
"province": "",
"last_updated": "2020-04-03T01:40:00.724616Z",
"coordinates": {
"latitude": "-14.235",
"longitude": "-51.9253"
},
"latest": {
"confirmed": 8044,
"deaths": 324,
"recovered": 0
},
"timelines": {
"confirmed": {
"latest": 8044,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"deaths": {
"latest": 324,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"recovered": {
"latest": 0,
"timeline": {}
}
}
}]"""
y = json.loads(x)
print(y)