简体   繁体   中英

Python Dictionary - Looping issue (nested) | TypeError: list indices must be integers or slices, not str

I am trying to loop over a nested dictionary but keep getting the error: list indices must be integers or slices, not str.

This is the data I am trying to loop over and have converted to a dictionary with using xmltodict:

<Report ReportName="Dummy Data">
<Elements>
    <DummyParameters>
        <DummyParameter Name="User" />
    </DummyParameters>
    <InstanceElement Name="Instance 1" ID="1d2cd23f-1324-eb11-a2f4-00155d641104">
    </InstanceElement>
    <InstanceElement Name="Instance 2" ID="1d2cd23f-1324-eb11-a2f4-00155d641105">
    </InstanceElement>
</Elements>
<Properties>
    <DummyParameters>
        <DummyParameter Name="User" Value="5914d71c-e0c7-e911-a2e0-00155d641201" />
    </DummyParameters>
    <InstanceElement Name="Instance 1" ID="1d2cd23f-1324-eb11-a2f4-00155d641104">
        <InstanceProperty InstanceProperty="A" Value="Value_A" ID="1f2cd23f-1324-eb11-a2f4-00155d641104" />
        <InstanceProperty InstanceProperty="B" Value="Value_B" ID="d214a7b7-6b6f-ea11-a2eb-00155d641104" />
        <InstanceProperty InstanceProperty="C" ID="ec14a7b7-6b6f-ea11-a2eb-00155d641104" />
        <InstanceProperty InstanceProperty="D" ID="de14a7b7-6b6f-ea11-a2eb-00155d641104" />
    </InstanceElement>
    <InstanceElement Name="Instance 2" CreatedBy="Test User 4" ID="1d2cd23f-1324-eb11-a2f4-00155d641105">
        <InstanceProperty InstanceProperty="A" Value="Value_A" ID="2f2cd23f-1324-eb11-a2f4-00155d641104"  />
        <InstanceProperty InstanceProperty="B" Value="Value_B" ID="2214a7b7-6b6f-ea11-a2eb-00155d641104"  />
        <InstanceProperty InstanceProperty="C" CreatedBy="Test User" ID="2c14a7b7-6b6f-ea11-a2eb-00155d641104" />
        <InstanceProperty InstanceProperty="D" CreatedBy="Test User" ID="2e14a7b7-6b6f-ea11-a2eb-00155d641104" />
    </InstanceElement>
</Properties>

Ordered Dict Structure

And the dict (as simple as I could) as text.

OrderedDict([('Report', OrderedDict([('@ReportName', 'Dummy Data'), ('Elements', OrderedDict([('DummyParameters', OrderedDict([('DummyParameter', OrderedDict([('@Name', 'User')]))])), ('InstanceElement', [OrderedDict([('@Name', 'Instance 1'), ('@ID', '1d2cd23f-1324-eb11-a2f4-00155d641104')]), OrderedDict([('@Name', 'Instance 2'), ('@ID', '1d2cd23f-1324-eb11-a2f4-00155d641105')])])])), ('Properties', OrderedDict([('DummyParameters', OrderedDict([('DummyParameter', OrderedDict([('@Name', 'User'), ('@Value', '5914d71c-e0c7-e911-a2e0-00155d641201')]))])), ('InstanceElement', [OrderedDict([('@Name', 'Instance 1'), ('@ID', '1d2cd23f-1324-eb11-a2f4-00155d641104'), ('InstanceProperty', [OrderedDict([('@InstanceProperty', 'A'), ('@Value', 'Value_A'), ('@ID', '1f2cd23f-1324-eb11-a2f4-00155d641104')]), OrderedDict([('@InstanceProperty', 'B'), ('@Value', 'Value_B'), ('@ID', 'd214a7b7-6b6f-ea11-a2eb-00155d641104')])])]), OrderedDict([('@Name', 'Instance 2'), ('@CreatedBy', 'Test User 4'), ('@ID', '1d2cd23f-1324-eb11-a2f4-00155d641105'), ('InstanceProperty', [OrderedDict([('@InstanceProperty', 'A'), ('@Value', 'Value_A'), ('@ID', '2f2cd23f-1324-eb11-a2f4-00155d641104')]), OrderedDict([('@InstanceProperty', 'B'), ('@Value', 'Value_B'), ('@ID', '2214a7b7-6b6f-ea11-a2eb-00155d641104')])])])])]))]))])

I am able to get IDs for the InstanceElements within the property branch by using this:

for item in data_dict["Report"]["Properties"]["InstanceElement"]:
ids['instance_id'] = [{value['@ID']} for value in data_dict["Report"]["Properties"]["InstanceElement"]]

However when I try to loop over the InstanceProperty (one level deeper) I can only do it by accessing it through an index:

for item in data_dict["Report"]["Properties"]["InstanceElement"][0]["InstanceProperty"]:
print(item)

Otherwise I get an error: TypeError: list indices must be integers or slices, not str. And I would like to go over all of the InstanceProperty items in the dictionary

How would I do this? As I am trying to get the ID of the InstanceElement assigned to all its children (InstanceProperty).

Thanks in advance!!

Mark

data_dict["Report"]["Properties"]["InstanceElement"] is a list, not a dict or an OrderedDict . If you want to print all InstanceProperties , you can use a nested loop to go through both lists:

for element in data_dict["Report"]["Properties"]["InstanceElement"]:
    for prop in element["InstanceProperty"]:
        print(prop)

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