【求助】如何分组统计json里面的数据
本帖最后由 xlinux 于 2022-9-7 20:57 编辑{'columns': ['family', 'proto', 'port', 'mac', 'ip', 'conns', 'rx_bytes', 'rx_pkts', 'tx_bytes', 'tx_pkts', 'layer7'],
'data': [
34:23:87:61:86:27', '192.168.5.134', 6779, 66049721, 68653, 7605216, 45729, 'HTTPS'],
'34:23:87:61:86:27', '192.168.5.134', 884, 22577571, 16577, 1008030, 19575, None],
34:23:87:61:86:27', '192.168.5.134', 3332, 4631492, 7600, 1118669, 6983, 'HTTP'],
,
,
,
34:23:87:61:86:27', '192.168.5.134', 4, 51, 1, 5112, 4, 'QUIC']]}
原始JSON数据如上所示,只是一部分的,全部的数据都是这个格式的。
需求:
主要是data键里面的值。根据第4个不同的元素 34:23:87:61:86:27,00:11:32:94:5b:9e 等分组,尔后统计每组中其第7个值和第9个值的总和,形成新的字典。
93258835 = 66049721+22577571 + 4631492 + 51
9737027 = 7605216 +1008030 +1118669 + 5112
102995862 = 93258835+ 9737027
最终效果(totaldata = updata + downdata):
{
'user1':{
"mac_add" : "34:23:87:61:86:27",
"updata" : "93258835",
"downdata":"9737027",
"totaldata":"102995862",
},
'user2':{
"mac_add" : "00:11:32:94:5b:9e",
"updata" : "xxx",
"downdata":"xxx",
"totaldata":"xxx",
},
'user3':{
"mac_add" : "64:09:80:4d:bb:b0",
"updata" : "xxx",
"downdata":"xxx",
"totaldata":"xxx",
},
}
求Python实现代码,非常感谢。 使用jsonpath 泽哥 发表于 2022-9-8 09:25
使用jsonpath
能不能帮忙写个示例,谢谢! 是这样吗?
from pprint import pprint
json_data = {'columns': ['family', 'proto', 'port', 'mac', 'ip', 'conns', 'rx_bytes', 'rx_pkts', 'tx_bytes', 'tx_pkts', 'layer7'],
'data': [
,
,
,
,
,
,
]}
dict_result = {}
for i in range(len(json_data['data'])):
for k in range(len(json_data['data'])):
mac_add = json_data['data']
updata = json_data['data']
downdata = json_data['data']
totaldata = updata + downdata
dict_result['user'+str(i+1)] = {
'mac_add':mac_add,
'updata':updata,
'downdata':downdata,
'totaldata':totaldata
}
pass
pass
pprint(dict_result)
本帖最后由 知心 于 2022-9-8 11:41 编辑
import json
data = {
'columns': ['family', 'proto', 'port', 'mac', 'ip', 'conns', 'rx_bytes', 'rx_pkts', 'tx_bytes', 'tx_pkts', 'layer7'],
'data': [
[4, 'TCP', 443, '34:23:87:61:86:27', '192.168.5.134',
6779, 66049721, 68653, 7605216, 45729, 'HTTPS'],
[4, 'IP', 0, '34:23:87:61:86:27', '192.168.5.134',
884, 22577571, 16577, 1008030, 19575, None],
[4, 'TCP', 80, '34:23:87:61:86:27', '192.168.5.134',
3332, 4631492, 7600, 1118669, 6983, 'HTTP'],
[4, 'TCP', 443, '00:11:32:94:5b:9e', '192.168.5.222',
1055, 2188864, 3920, 326644, 4072, 'HTTPS'],
[4, 'IP', 0, '00:11:32:94:5b:9e', '192.168.5.222',
2551, 49020, 620, 300476, 2151, None],
[4, 'IP', 0, '64:09:80:4d:bb:b0', '192.168.43.201',
500, 11704, 154, 13452, 177, None],
[4, 'UDP', 443, '34:23:87:61:86:27',
'192.168.5.134', 4, 51, 1, 5112, 4, 'QUIC']
]
}
def first(datas):
'''
根据第4个不同的元素进行分组
'''
dist1 = {}
for data in datas:
if data not in dist1:
dist1] =
else:
dist1].append(data)
return dist1
def second(datas):
'''
统计updata(第7个元素),downdata(第9个元素),totaldata(updata+downdata),合并成最终格式
'''
end_data = {}
for idx,mac_add in enumerate(datas):
updata = 0
downdata = 0
for item in datas:
updata +=item
downdata +=item
end_data['user'+str(idx+1)]={
"mac_add":mac_add,
"updata":str(updata),
"downdata":str(downdata),
"totaldata":str(updata+downdata)
}
return json.dumps(end_data,indent=4)
if __name__ == "__main__":
first_data = first(data["data"])
second_data = second(first_data)
print(second_data)
结果
{
"user1": {
"mac_add": "34:23:87:61:86:27",
"updata": "93258835",
"downdata": "9737027",
"totaldata": "102995862"
},
"user2": {
"mac_add": "00:11:32:94:5b:9e",
"updata": "2237884",
"downdata": "627120",
"totaldata": "2865004"
},
"user3": {
"mac_add": "64:09:80:4d:bb:b0",
"updata": "11704",
"downdata": "13452",
"totaldata": "25156"
}
} mmaaiiooo 发表于 2022-9-8 11:08
是这样吗?
from pprint import pprint
谢谢指导! 知心 发表于 2022-9-8 11:28
import json
data = {
谢谢指导! 我这来学习的
页:
[1]