-2

I need to extract the name, pattern, and ownerUserName out of this list of dictionaries and store the values as variables. This looks to be a list of dictionaries that are nested many layers deep so unsure how to extract them.

I'm running Python 2.7.9 on Windows and using Suds to make soap calls into Cisco Call Manager 10.5.

I'm fairly new to python so any help would be much appreciated!

Function to call 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

Code to call the function and produce a list of phones:

phones = ['SEPC40ACB4CBB76','SEP204C9ED76520']    

phonelist = []

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

print phonelist

Result

This is the result from:

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"
            }
      }
 }]

Edit

Trying to extract the values name, pattern, and ownerUserName using a for loop and setting the variable.. Keep getting Traceback and unsure what I'm doing wrong.

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

Getting Traceback:

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
lee17
  • 11
  • 2

1 Answers1

0

I finally got this to work with this code. Is this the best way to do it?

print '\n\nPhone List\n\n'

for i in phonelist: 
    name =  (str(i['return']['phone']['name']))
    pattern =  (str(i['return']['phone']['lines']['line'][0]['dirn']['pattern']))   
    ownerIdcheck =  (str(i['return']['phone']['ownerUserName']))
    if len(ownerIdcheck) > 0:
        ownerId =  (str(i['return']['phone']['ownerUserName']['value']))
    else: 
        ownerId = ''
    print name, ' | ', pattern , ' | ',ownerId
lee17
  • 11
  • 2