如何在python的elasticsearch中对json文件的特定值求和?

2024-04-16 21:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的JSON文件,我想对number key的所有值求和。如何使用Python实现这一点?你知道吗

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 7,
        "max_score": 1.0,
        "hits": [{
            "_index": "test",
            "_type": "json",
            "_id": "5878",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "1548",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "2751",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "8363",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "551",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "2195",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }, {
            "_index": "test",
            "_type": "json",
            "_id": "2990",
            "_score": 1.0,
            "_source": {
                "data_type": "click",
                "number": 1,
                "date": "1397/05/14",
                "host_id": "1231"
            }
        }]
    }
}

Tags: testidjsonhostnumbersourcedatadate
1条回答
网友
1楼 · 发布于 2024-04-16 21:14:02
import json

data = '{"took": 0, "timed_out": false, "_shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {"total": 7, "max_score": 1.0, "hits": [{"_index": "test", "_type": "json", "_id": "5878", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "1548", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2751", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "8363", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "551", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2195", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2990", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }] } } '
myk = json.loads(data)

count = 0
target = myk['hits']['hits']
for l in target:
    for k in l:
        if k == "_source":
            count += l[k]['number']
print(count)

输出:

7
>>> 

从json文件加载数据

import json


with open('data.json') as f:
    data = json.load(f)

count = 0
target = data['hits']['hits']
for l in target:
    for k in l:
        if k == "_source":
            count += l[k]['number']
print(count)


7
>>> 

相关问题 更多 >