python从复杂的嵌套字典和列表中提取值

时间:2017-03-28 06:19:36

标签: python python-2.7

虽然,我遇到了很多嵌套词典的例子,但我无法破解这个!以json格式和我想要的输出输入如下。 输入字典有超过100个条目,我想提取几个选定变量的值(如IP地址,Med,Active)。

对于某些条目,'med'嵌套在[0,1,2 etc]

的列表中
  

med = bgp ['10 .0.0.0 / 16'] ['bgpRoutePaths'] [0] .get('med')
  med = bgp ['10 .0.0.0 / 16'] ['bgpRoutePaths'] [1] .get('med')

Input:

    {u'10.0.0.0/16': {u'address': u'10.0.0.0',
                          u'bgpAdvertisedPeerGroups': {},
                          u'bgpRoutePaths': [{u'asPathEntry': {u'asPath': u'65404,65315 i',              
                                                               u'asPathType': None},
                                              u'localPreference': 100,
                                              u'med': 0,
                                              u'nextHop': u'172.1.1.169',
                                              u'routeType': {u'active': True,
                                                             u'atomicAggregator': False,
                                                             u'backup': False,
                                                             u'ecmp': False,
                                                             u'ecmpContributor': False,
                                                             u'ecmpHead': False,
                                                             u'queued': False,
                                                             u'stale': False,
                                                             u'suppressed': False,
                                                             u'ucmp': False,
                                                             u'valid': True},
                                              u'weight': 0},
                                             {u'asPathEntry': {u'asPath': u'65407  65315 65317 65000 i',
                                                               u'asPathType': None},
                                              u'localPreference': 100,
                                              u'med': 0,
                                              u'nextHop': u'172.16.1.94',
                                              u'routeType': {u'active': False,
                                                             u'atomicAggregator': False,
                                                             u'backup': False,
                                                             u'ecmp': False,
                                                             u'ecmpContributor': False,
                                                             u'ecmpHead': False,
                                                             u'queued': False,
                                                             u'stale': False,
                                                             u'suppressed': False,
                                                             u'ucmp': False,
                                                             u'valid': True},
                                              u'weight': 0}],
                          u'maskLength': 16},
}

输出:

IPaddr         asPath                               med  active
10.0.0.0/16    (65404 65315 i)                       0    True
10.0.0.0/16    (65407 65315 65317 65000 i)           0    False

2 个答案:

答案 0 :(得分:0)

将您的字典视为变量' a'然后你可以通过

访问数据
print a.keys()[0]
print a['10.0.0.0/16']['bgpRoutePaths'][0]['asPathEntry']['asPath'], ' ', a['10.0.0.0/16']['bgpRoutePaths'][0]['med'], ' ', a['10.0.0.0/16']['bgpRoutePaths'][0]['routeType']['active']


print a['10.0.0.0/16']['bgpRoutePaths'][1]['asPathEntry']['asPath'], ' ', a['10.0.0.0/16']['bgpRoutePaths'][1]['med'], ' ', a['10.0.0.0/16']['bgpRoutePaths'][1]['routeType']['active']

答案 1 :(得分:0)

我能够自己解决这个问题!

  for k,v in a.iteritems():
  add = k
  for i in range (0,len(a.values()[0]['bgpRoutePaths'])):
   aspath = a.values()[0]['bgpRoutePaths'][i]['asPathEntry'].get('asPath')
   active = a.values()[0]['bgpRoutePaths'][i]['routeType'].get('active')
   print add,aspath,med,active

Output:
10.0.0.0/16 65404,65315 i 0 True
10.0.0.0/16 65407  65315 65317 65000 i 0 False
相关问题