如何在python中使用json提取数据

2024-09-28 21:35:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我写的代码有问题。当我运行代码时,我得到一个错误:

Traceback (most recent call last):
File "test23_json_users.py", line 23, in <module>
for user_dict in abc['user_account']['sip_id']:
TypeError: list indices must be integers, not str

代码:

^{pr2}$

Tags: 代码inpyjsonmost错误linecall
2条回答

abc['user_account']应该是一个列表。在

列表a=['hello',1,2,'people']有4个元素由索引a[0]a[3]访问。在

字典d={'a':1,'b':2}有键和值。在本例中,d['a']的值为1,d[b']的值为2。在

如果要访问abc['user_account']中的内容,请键入abc['user_account'][42]。如果你想迭代它

for a in abc['user_account']:
  print a

改变

for user_dict in abc['user_account']['sip_id']: 

^{pr2}$

相关问题 更多 >