在python中从嵌套的json数据存储数据

时间:2019-06-21 09:37:17

标签: python json nested

我想从此json数据的所有集中访问'dl_dst'值 我可以获取dl_dst数据,但每个数据点背对背存储3次。我犯了什么错误?

{'1': [{'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 833000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:02',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 828000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:02',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 809000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:03',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 807000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:03',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 787000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:04',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 786000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:04',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:CONTROLLER'],
    'byte_count': 1944,
    'cookie': 0,
    'duration_nsec': 582000000,
    'duration_sec': 168,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 80,
    'match': {},
    'packet_count': 27,
    'priority': 0,
    'table_id': 0}]}

这是我编写的python代码

import requests
import pprint
import json


url = 'https://api.myjson.com/bins/19yp59'
get_data = requests.get(url)
get_data_json = get_data.json()
data = get_data_json['1']

Dest_Mac = []

for k in data:

 for i in k['match']:
     Dest_Mac.append(k['match']['dl_dst'])


print(Dest_Mac)

这是此代码的结果

['00:00:00:00:00:01','00:00:00:00:00:01','00:00:00:00:00:01','00:00 :00:00:00:02','00:00:00:00:00:02','00:00:00:00:00:02','00:00:00:00:00:01 ','00:00:00:00:00:01','00:00:00:00:00:01','00:00:00:00:00:03','00:00:00 :00:00:03','00:00:00:00:00:03','00:00:00:00:00:01','00:00:00:00:00:01', '00:00:00:00:00:01','00:00:00:00:00:04','00:00:00:00:00:04','00:00:00:00 :00:04']

请注意,每个数据点被打印3次。

4 个答案:

答案 0 :(得分:0)

欢迎来到Stackoverflow!

您的内部循环遍历每个match字典中的键,但是每次找到新键时都会打印与dl_dts键相关联的值-因此,每个值的三个重复。 / p>

根本不需要迭代:您已经有了字典,因此您所要做的就是打印出该键的值-假设它存在。

因此,此代码使用dict的get方法在键缺失且没有任何内容可添加到列表时返回None

data = {'1': [{'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 833000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:02',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
# remainder of data omitted for brevity ...
   {'actions': ['OUTPUT:CONTROLLER'],
    'byte_count': 1944,
    'cookie': 0,
    'duration_nsec': 582000000,
    'duration_sec': 168,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 80,
    'match': {},
    'packet_count': 27,
    'priority': 0,
    'table_id': 0}]}

result = []
for item in data['1']:
    this = item['match'].get('dl_dst')
    if this:
        result.append(this)

print(result)

这似乎可以提供您想要的输出:

['00:00:00:00:00:01', '00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04']

答案 1 :(得分:0)

将for循环更改为以下内容:

for k in data:
    for i,j in k['match'].items():
        if i == "dl_dst":
            Dest_Mac.append(j)

print(Dest_Mac)

输出:

['00:00:00:00:00:01', '00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04']

问题在于您的内部for循环。每次找到新密钥时,它都会打印 dl_dst 的值。

希望这能回答您的问题!

答案 2 :(得分:0)

通过执行以下操作:

for i in k['match']:
    Dest_Mac.append(k['match']['dl_dst'])

您正在k['match']中存在的所有键中进行迭代,因此每次都附加k['match']['dl_dst']。在您的示例中,它使所需的输出增加了三倍。

您应该替换为:

if 'dl_dst' in k['match'].keys():
        Dest_Mac.append(k['match']['dl_dst'])

输出

['00:00:00:00:00:01', '00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04']

答案 3 :(得分:0)

尝试一下

>>> data_json = {'1': [{'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 833000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:02',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 828000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:02',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 809000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:03',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 807000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:03',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:1'],
    'byte_count': 238,
    'cookie': 0,
    'duration_nsec': 787000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:01',
              'dl_src': '00:00:00:00:00:04',
              'in_port': 2},
    'packet_count': 3,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:2'],
    'byte_count': 140,
    'cookie': 0,
    'duration_nsec': 786000000,
    'duration_sec': 138,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 104,
    'match': {'dl_dst': '00:00:00:00:00:04',
              'dl_src': '00:00:00:00:00:01',
              'in_port': 1},
    'packet_count': 2,
    'priority': 1,
    'table_id': 0},
   {'actions': ['OUTPUT:CONTROLLER'],
    'byte_count': 1944,
    'cookie': 0,
    'duration_nsec': 582000000,
    'duration_sec': 168,
    'flags': 0,
    'hard_timeout': 0,
    'idle_timeout': 0,
    'length': 80,
    'match': {},
    'packet_count': 27,
    'priority': 0,
    'table_id': 0}]}

输出:

>>> out = []
>>> data = data_json[1]
>>> for d in data:
    for k,v in d.items():
        if k == 'match':
            for ik,iv in v.items():
                if ik == 'dl_dst':
                    out.append(iv)

使用列表理解:

>>> [iv for d in data for k,v in d.items() if k=='match' for ik,iv in v.items() if ik == 'dl_dst']
['00:00:00:00:00:01', '00:00:00:00:00:02', '00:00:00:00:00:01', '00:00:00:00:00:03', '00:00:00:00:00:01', '00:00:00:00:00:04']