Как сложить конкретные значения моего файла JSON вasticsearch в Python? - PullRequest
0 голосов
/ 27 августа 2018

Это мой JSON-файл, и я хочу суммировать все значения числового ключа. Как я могу сделать это с 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"
            }
        }]
    }
}

1 Ответ

0 голосов
/ 27 августа 2018
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
>>> 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...