仅从json中提取与良好列列表匹配的值

时间:2018-01-20 11:24:29

标签: python json

我正在python中读取一个csv文件并提取包含json数据的列并提取所需的键值对。所以我在列表中给出了我想要的好列(例如good_columns =[])列表,并且我将该键的值附加到新列表中(比如说data=[])但是在json中有一些键在那里我不希望根据我的好列表列表,我想在我的数据列表中添加零。 提前谢谢。

good_columns = [
    "outboundrtp_roundTripTime",
    "outboundrtp_targetBitrate",
    "outboundrtp_bytesSent",
    "outboundrtp_packetsSent",
    "outboundrtp_packetsLost",
    "inboundrtp_inboundJitter" ,
    "inboundrtp_bytesReceived" ,
    "inboundrtp_packetsLost"
    ]

json内容:

{
"endpoint_AudioE2ELatency": 0,
"endpoint_VideoE2ELatency": 165,
"outboundrtp_roundTripTime": 0.340,
"outboundrtp_targetBitrate": 17586,
"outboundrtp_bytesSent": 8880,
"outboundrtp_packetsSent": 78,
"outboundrtp_packetsLost": 1,
"outboundrtp_remb": 0,
"inboundrtp_inboundJitter": 0.024,
"inboundrtp_bytesReceived": 24835,
"inboundrtp_packetsLost": 0,
"inboundrtp_remb": 0
}

{
"endpoint_AudioE2ELatency": 0,
"endpoint_VideoE2ELatency": 149,
"outboundrtp_roundTripTime": 0,
"outboundrtp_targetBitrate": 5616,
"outboundrtp_bytesSent": 37366,
"outboundrtp_packetsSent": 654,
"outboundrtp_packetsLost": 0,
"outboundrtp_remb": 0
}

我希望我的列表包含值为0的键,而不是良好的列。所以它应该给出如下结果: 为第一个:
期望的输出:

[0.340,17586,8880,78,1,0.024,24835,0]

当前输出:

[0,0,0.340,17586,8880,78,1,0]

第二个:
期望的输出:

[0,5616,37366,654,0,0,0] 

当前输出:

[0,0,0,5616,37366,654,0]  

我尝试了什么:

for key, value in record.items():
    if key in good_columns:
        data.append(value)
    elif key not in good_columns:
        data.append(0)

但这并没有给出理想的输出。

2 个答案:

答案 0 :(得分:0)

试试这个:

# record = json.loads()

for key in good_columns:
    if(key not in record.keys()):
        record[key] = 0

答案 1 :(得分:0)

good_columns = [
    "outboundrtp_roundTripTime",
    "outboundrtp_targetBitrate",
    "outboundrtp_bytesSent",
    "outboundrtp_packetsSent",
    "outboundrtp_packetsLost",
    "inboundrtp_inboundJitter" ,
    "inboundrtp_bytesReceived" ,
    "inboundrtp_packetsLost"
    ]
json_data = [{
"endpoint_AudioE2ELatency": 0,
"endpoint_VideoE2ELatency": 165,
"outboundrtp_roundTripTime": 0.340,
"outboundrtp_targetBitrate": 17586,
"outboundrtp_bytesSent": 8880,
"outboundrtp_packetsSent": 78,
"outboundrtp_packetsLost": 1,
"outboundrtp_remb": 0,
"inboundrtp_inboundJitter": 0.024,
"inboundrtp_bytesReceived": 24835,
"inboundrtp_packetsLost": 0,
"inboundrtp_remb": 0
},{
"endpoint_AudioE2ELatency": 0,
"endpoint_VideoE2ELatency": 149,
"outboundrtp_roundTripTime": 0,
"outboundrtp_targetBitrate": 5616,
"outboundrtp_bytesSent": 37366,
"outboundrtp_packetsSent": 654,
"outboundrtp_packetsLost": 0,
"outboundrtp_remb": 0
}]

def get_val(json_dict, val):
    return json_dict.get(val,0)

for index, json_dict in enumerate(json_data,1):
    merge_keys = good_columns + list(json_dict.keys())
    order_keys = list(dict.fromkeys(merge_keys))
    data = [get_val(json_dict,val) for val in order_keys]
    print('{}.Dictionary data is {}'.format(index,data))

>>>1.Dictionary data is [0.34, 17586, 8880, 78, 1, 0.024, 24835, 0, 0, 165, 0, 0]
   2.Dictionary data is [0, 5616, 37366, 654, 0, 0, 0, 0, 0, 149, 0]

希望它可以帮到你。