如何从python中的嵌套字典列表中提取值
我需要从这个字典列表中提取名称、模式和ownerUserName,并将这些值存储为变量。这看起来是一个字典列表,嵌套了很多层,因此不确定如何提取它们 我在Windows上运行Python2.7.9,并使用sud对Cisco Call Manager 10.5进行soap调用 我是python新手,因此非常感谢您的帮助 调用CUCM的函数:如何从python中的嵌套字典列表中提取值,python,list,dictionary,nested,suds,Python,List,Dictionary,Nested,Suds,我需要从这个字典列表中提取名称、模式和ownerUserName,并将这些值存储为变量。这看起来是一个字典列表,嵌套了很多层,因此不确定如何提取它们 我在Windows上运行Python2.7.9,并使用sud对Cisco Call Manager 10.5进行soap调用 我是python新手,因此非常感谢您的帮助 调用CUCM的函数: def getPhone(name): count = 0 list = [] #name = raw_input('Enter Phone Name: ')
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
结果
这是由于:
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并设置变量。。不断追踪,不确定我做错了什么
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
回溯(最近一次呼叫最后一次):
文件“ownerIDUpdateTest.py”,第88行,在
对于电话列表中的i['return']['phone']:
TypeError:列表索引必须是整数,而不是str
我终于用这段代码实现了这一点。这是最好的方法吗
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
这是JSON吗?@MorganThrapp-def不是JSON(返回)。。我不知道这是什么这根本不合法syntax@FirebladeDan哦,是的,我错过了。我非常困惑,好像是肥皂。看看
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