如何从python中的嵌套字典列表中提取值

时间:2015-07-24 19:38:54

标签: python list dictionary nested suds

我需要从这个词典列表中提取名称,模式和ownerUserName,并将值存储为变量。这看起来是一个嵌套很多层深度的字典列表,所以不确定如何提取它们。

我在Windows上运行Python 2.7.9并使用Suds拨打肥皂呼叫到Cisco Call Manager 10.5。

我对python很新,所以任何帮助都会非常感激!

调用CUCM的函数:

def getPhone(name):
count = 0
list = []
#name = raw_input('Enter Phone Name: ')
method = 'getPhone'
#result = axl.client.service.getPhone({'name':'test123456'},returnedTags={'name':'','description':''}) <- Not working Had to build custom envelope. 
#return result
    def getPhoneEnvelope(name):
       SOAP = '<SOAP-ENV:Envelope xmlns:ns0="http://www.cisco.com/AXL/API/10.5" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">'
       SOAP += '<SOAP-ENV:Header/>'
       SOAP += '<ns1:Body>'
       SOAP += '<ns0:getPhone>'
       SOAP += '<name>' + name + '</name>'
       SOAP +=      '<returnedTags>'
       SOAP +=          '<name></name>'
       SOAP +=          '<lines>'
       SOAP +=              '<line><index></index><dirn><pattern></pattern><routePartitionName></routePartitionName></dirn></line>'
       SOAP +=          '</lines>'
       SOAP +=          '<ownerUserName></ownerUserName>'
       SOAP +=      '</returnedTags>'
       SOAP += '</ns0:getPhone>'
       SOAP += '</ns1:Body>'
       SOAP += '</SOAP-ENV:Envelope>'
       return SOAP

result = axl.client.service.getPhone(__inject={'msg':getPhoneEnvelope(name)})
return result

调用该函数并生成电话列表的代码:

phones = ['SEPC40ACB4CBB76','SEP204C9ED76520']    

phonelist = []

for i in phones[:2]:
    name = i
    print name
    phone = getPhone(name)
    phonelist.append(phone)

print phonelist

结果

这是以下结果:

print phonelist


#########################
This is the result: 

[(reply){
   return =
      (return){
         phone =
            (RPhone){
               _ctiid = 1
               _uuid = "{0003A0AF-DFA1-B37D-CC47-921E7F85856B}"
               name = "SEPC40ACB4CBB76"
               lines =
                  (lines){
                     line[] =
                        (RPhoneLine){
                           _uuid = "{57ECE3BA-6510-B633-A27D-209CF5A7D20E}"
                           index = "1"
                           dirn =
                              (RDirn){
                                 _uuid = "{1C1C0882-E1C5-A157-4FD7-7CA4CEEA8B29}"
                                 pattern = "5551234567"
                                 routePartitionName =
                                    (routePartitionName){
                                       value = "Global-All-Lines"
                                       _uuid = "{7DB49512-3679-0B0A-D5E2-A8121BD42DFC}"
                                    }
                              }
                        },
                  }
               ownerUserName = "first.last1"
            }
      }
 }, (reply){
   return =
      (return){
         phone =
            (RPhone){
               _ctiid = 2
               _uuid = "{00063BF8-E71E-3211-B9F6-B5DABC274FFA}"
               name = "SEP204C9ED76520"
               lines =
                  (lines){
                     line[] =
                        (RPhoneLine){
                           _uuid = "{2BDB0C1C-0DAA-1A3D-B36D-21DA8E788548}"
                           index = "1"
                           dirn =
                              (RDirn){
                                 _uuid = "{294C8B59-0059-E30C-28F8-3CA36F3CAE25}"
                                 pattern = "5551235678"
                                 routePartitionName =
                                    (routePartitionName){
                                       value = "Global-All-Lines"
                                       _uuid = "{7DB49512-3679-0B0A-D5E2-A8121BD42DFC}"
                                    }
                              }
                        },
                  }
               ownerUserName = "first.last2"
            }
      }
 }]

编辑

尝试使用for循环提取值name,pattern和ownerUserName并设置变量..继续获取Traceback并不确定我做错了什么。

for i in phonelist['return']['phone']: 
    phonename = i['name']
    pattern = i['lines']['line']['dirn']['pattern']
    ownerid = i['ownerUserName']
    print phonename
    print pattern
    print ownerid

获取回溯:

Traceback (most recent call last):
 File "ownerIDUpdateTest.py", line 88, in <module>
  for i in phonelist['return']['phone']:
TypeError: list indices must be integers, not str

1 个答案:

答案 0 :(得分:0)

我终于得到了这个代码。这是最好的方法吗?

ServerSession