简体   繁体   English

迭代字典列表

[英]Iterating over a list of dictionaries

JSON JSON

Newbie here.新手来了I want to iterate over this list of dictionaries to get the "SUPPLIER" for every dictionary.我想遍历这个字典列表以获得每个字典的“供应商”。 I tried我试过

turbine_json_path = '_maps/turbine/turbine_payload.json'
with open(turbine_json_path, "r") as f:
    turbine = json.load(f)
    # print((turbine))

    for supplier in turbine[0]['GENERAL']:
        print(supplier["SUPPLIER"])

But I get a type error.. TypeError: string indices must be integers但我得到一个类型错误.. TypeError: 字符串索引必须是整数

Any help is appreciated.任何帮助表示赞赏。

There is only one supplier key in your dictionary, so it would be你的字典中只有一个供应商键,所以它是

supplier = turbine[0]['GENERAL']['SUPPLIER']

Other wise your for loop is looping over the keys within the 'GENERAL' dictionary, which are strings.否则,您的 for 循环将遍历'GENERAL'字典中的键,这些键是字符串。

for d in turbine:
    print(d["GENERAL"]["SUPPLIER"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM