gpt4 book ai didi

Python 从 json 中打印所有内容

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:44 25 4
gpt4 key购买 nike

我可以使用以下代码提取列表中所有艺术家中的一位艺术家。

print (data['topartists']['artist'][0]['name'])

现在我想要打印品返回所有艺术家的名字。

我会假设使用:

for i in data:
print (data['topartists']['artist'][i]['name'])

但是这会导致错误:TypeError: list indices must be integers, not unicode

我应该添加什么来打印所有艺术家?

最佳答案

您应该迭代艺术家,而不是数据本身。

for i in data['topartists']['artist']:
print (i['name'])

关于Python 从 json 中打印所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com