#python从列表中提取信息

2024-05-20 08:41:13 发布

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

不是Python专家。。。我想从列表中提取信息。 名单如下: enter image description here

这是我的代码:

    for item in data ["items"]:
        for p in item ["standings"]["clan"]["participants"]:
                print("%s %s" % (
                    p["tag"],
                    p["name"],
))

出现此错误时:

  File "_players_.py", line 26, in <module>
    for p in item ["standings"]["clan"]["participants"]:
TypeError: list indices must be integers or slices, not str

需要帮忙吗

图片的另一个大视图: enter image description here

谢谢


Tags: 代码in信息列表fordatatagitems
2条回答

我认为这是一个.json文件,您可以尝试以下方法:

print(json_file['items'][0]['standings'][0]['clan']['participants'][number of your information])

希望这有帮助:-)

看看你的图片,item["standings"]会产生一个列表,而不是一本字典。这将由错误确认,该错误详细说明列表是使用字符串编制索引的。所以,我想你会想要一些大致如下的东西:

...
for s in item["standings"]:
    for p in s["clan"]["participants"]:
        ...

相关问题 更多 >