【发布时间】:2020-08-20 05:16:16
【问题描述】:
我正在尝试将 JSON URL 中的数据提取到 pandas 中,但该文件有多个列表和字典“层”,我似乎无法导航。
import json
from urllib.request import urlopen
with urlopen('https://statdata.pgatour.com/r/010/2020/player_stats.json') as response:
source = response.read()
data = json.loads(source)
for item in data['tournament']['players']:
pid = item['pid']
statId = item['stats']['statId']
name = item['stats']['name']
tValue = item['stats']['tValue']
print(pid, statId, name, tValue)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-84-eadd8bdb34cb> in <module>
1 for item in data['tournament']['players']:
2 player_id = item['pid']
----> 3 stat_id = item['stats']['statId']
4 stat_name = item['stats']['name']
5 stat_value = item['stats']['tValue']
TypeError: list indices must be integers or slices, not str
我想要得到的输出是这样的:-
【问题讨论】: