Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

python - TypeError: list indices must be integers, not dict

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...