简体   繁体   中英

Iterating over a list of dictionaries

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

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 d in turbine:
    print(d["GENERAL"]["SUPPLIER"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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