吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1133|回复: 7
收起左侧

[求助] 【求助】如何分组统计json里面的数据

[复制链接]
xlinux 发表于 2022-9-7 20:56
本帖最后由 xlinux 于 2022-9-7 20:57 编辑

{'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']]}


原始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实现代码,非常感谢。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

泽哥 发表于 2022-9-8 09:25
使用jsonpath
 楼主| xlinux 发表于 2022-9-8 10:07
mmaaiiooo 发表于 2022-9-8 11:08
是这样吗?
[Python] 纯文本查看 复制代码
from pprint import pprint

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']]}

dict_result = {}

for i in range(len(json_data['data'])):
    for k in range(len(json_data['data'][i])):
        mac_add = json_data['data'][i][3]
        updata = json_data['data'][i][6]
        downdata = json_data['data'][i][8]
        totaldata = updata + downdata
        dict_result['user'+str(i+1)] = {
            'mac_add':mac_add,
            'updata':updata,
            'downdata':downdata,
            'totaldata':totaldata
        }
        pass
    pass
pprint(dict_result)


微信截图_20220908110604.png
知心 发表于 2022-9-8 11:28
本帖最后由 知心 于 2022-9-8 11:41 编辑

[Python] 纯文本查看 复制代码
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[3] not in dist1:
            dist1[data[3]] = [data]
        else:
            dist1[data[3]].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[mac_add]:
            updata +=item[6]
            downdata +=item[8]

        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)


结果
[JavaScript] 纯文本查看 复制代码
{
    "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"
    }
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yjn866y + 1 + 1 热心回复!

查看全部评分

 楼主| xlinux 发表于 2022-9-8 11:53
mmaaiiooo 发表于 2022-9-8 11:08
是这样吗?
[mw_shl_code=python,true]from pprint import pprint

谢谢指导!
 楼主| xlinux 发表于 2022-9-8 11:54
知心 发表于 2022-9-8 11:28
[mw_shl_code=python,true]import json

data = {

谢谢指导!
yjn866y 发表于 2022-9-8 19:46
我这来学习的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:08

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表