Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: list indices must be integers, not dict

Tags:

python

My json file look likes this and I'm trying to access the element syslog in a for loop.

{
  "cleanup":{
    "folderpath":"/home/FBML7HR/logs",
    "logfilename":""
  },
  "preparation":{
    "configuration":{
      "src_configfile":"src.cfg",
      "dest_configfile":"/var/home/FBML7HR/etc/vxn.cfg"
    },
    "executable_info1":[
      {
        "login_info":{
          "hostname":"10.4.0.xxx",
          "username":"***",
          "password":"***"
        }
      },
      {
        "command":{
          "folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/test/hostsim/",
          "processname":"northhostsim",
          "parameters":"-d"
        }
      }
    ],
    "executable_info2":[
      {
        "login_info":{
          "hostname":"10.4.0.xxx",
          "username":"***",
          "password":"***"
        }
      },
      {
        "command":{
          "folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/build/Linux-2.6.18-194.8.1.el5/bin",
          "processname":"northhost",
          "parameters":"-s brazil -d"
        }
      }
    ],
    "executable_info3":[
      {
        "login_info":{
          "hostname":"10.4.0.xxx",
          "username":"***",
          "password":"***"
        }
      },
      {
        "command":{
          "folderpath":"cd /var/home/xxx/SrcCode/vxnservers/fdchost/north/test/vxnclient_mt",
          "processname":"vxnclient_north_mt",
          "parameters":"0 320 205 14897 16880 60000 60000 2 2"
        }
      }
    ]
  },
  "execution":[
    {
      "test_case":{
        "scriptname":"/var/home/FBML7HR/test/testcase1.sh",
        "testreport":{
          "syslog":"/var/log/messages",
          "backupsyslog":"backuplogs1.txt",
          "clientsimlog":"/var/home/FBML7HR/test/out.log",
          "backupclientsimlog":"Clientlogs1.txt"
        }
      }
    },
    {
      "test_case":{
        "scriptname":"/var/home/FBML7HR/test/testcase2.sh",
        "testreport":{
          "syslog":"/var/log/messages",
          "backupsyslog":"backuplogs2.txt",
          "clientsimlog":"/var/home/FBML7HR/test/out.log",
          "backupclientsimlog":"Clientlogs2.txt"
        }
      }
    }
  ],
  "verification":{
    "testreport":{
      "syslog":"/var/log/messages",
      "backupsyslog":"backuplogs.txt",
      "reportfilename":"/var/home/FBML7HR/test/out.log",
      "backuplogfile":"Clientlogs.txt"
    }
  }
}

I do it like this:

for i in data['execution']:
    cmd = data['execution'][i]['test_case']['scriptname']

But I get the error saying "TypeError: list indices must be integers, not dict". I'm new to python (and json as well). Could anybody suggest where I'm going wrong ?

like image 299
user1124236 Avatar asked Oct 08 '14 21:10

user1124236


People also ask

How do you fix list indices must be integers or slices not dict?

The Python "TypeError: list indices must be integers or slices, not dict" occurs when we use a dictionary to access a list at a specific index. To solve the error, use an integer or a slice for list indexes, e.g. my_list[0] .

How do I fix error list indices must be integers or slices not str?

The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index. To solve the error, use the int() class to convert the string to an integer, e.g. my_list[int(my_str)] .

How do I fix TypeError list indices must be integers or slices not tuple?

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.

What is a list of dictionaries Python?

What is a list of dictionaries in Python? We all know in Python, a list is a linear data structure that can store a collection of values in an ordered manner. And these values can be any Python object. A dictionary is also a Python object which stores the data in the key:value format.


2 Answers

You are looping over the values in the list referenced by data['execution'], not indices.

Just use those values (dictionaries) directly:

for i in data['execution']:
    cmd = i['test_case']['scriptname']

You probably want to give that a more meaningful loop name:

for entry in data['execution']:
    cmd = entry['test_case']['scriptname']
like image 113
Martijn Pieters Avatar answered Sep 28 '22 19:09

Martijn Pieters


  • you can try this, it works for me
dataArray=data['execution']
for i in range(len(dataArray)):
    cmd = dataArray[i]['test_case']['scriptname']
  • This exactly loops by indices, so there will not be any confusion,
  • For me this is very simple and understandable
like image 32
Rohan Devaki Avatar answered Sep 28 '22 21:09

Rohan Devaki