Python解析JSON表查找值和增量计数器

2024-05-16 11:31:12 发布

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

我想在这里找到一个解决方案,有点匆忙,否则,我会发布更多的解决方案,但前提是。。你知道吗

我尝试使用Python解析JSON表,其中包含事件的时间和事件的名称。每次出现一个特定的“名称”时,我希望它的相对计数器为该日期增加1—这样我就可以计算每天事件发生的次数(不在乎分钟数)。在一个文件夹中也有几个JSON文件,我想迭代一下。*.json文件

JSON的exmaple是:

{
  "logs" : [ {
    "other" : "xVKNXCVNsk",
    "time" : "2017-06-15T01:31:50.412Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Alpha Beta: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2017-06-15T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2017-06-15T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }]
}

所以在这个例子中,结果是:

"Alpha Beta: Bingo": 1 for 2017-06-15
"Terra Zaba: Bingo": 2 for 2017-06-15

非常感谢您的帮助!你知道吗


Tags: 文件namealpha名称jsontime事件解决方案
1条回答
网友
1楼 · 发布于 2024-05-16 11:31:12

首先需要重新格式化JSON数据,这里提供的包含一些错误。你知道吗

为了回答这个问题,我将JSON文件格式化如下。你知道吗

{
  "logs" : [ {
    "other" : "xVKNXCVNsk",
    "time" : "2017-06-15T01:31:50.412Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Alpha Beta: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2017-06-15T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2017-06-1T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }]
}

格式化JSON文件后,我可以试着回答这个问题。你知道吗

import json
from pprint import pprint

PATH = r"E:\temp\temp.json"

def parse_func(f):
    """
    parse the JSON file
    :param f: the parsed file path
    :return: the result dict
    """
    parse_Dict = dict()
    parse_List = list()
    num_Dict = dict()

    # load the JSON data to `data `
    with open(f) as data_file:
        data = json.load(data_file)

    # parse the "name " and the "time "
    for i in range(0, len(data["logs"])):
        parse_List.append(data["logs"][i]["name"])
        parse_List.append(data["logs"][i]["time"].split("T")[0])

    print(parse_List)

    # change the list to the dict
    for ii in range(0, len(parse_List), 2):
        # the "name " and the "time "
        parse_Dict[parse_List[ii]] = parse_List[ii + 1]

        # the "name " and the "retry number "
        if parse_List[ii] not in num_Dict:
            num_Dict[parse_List[ii]] = 1
        else:
            num_Dict[parse_List[ii]] = num_Dict[parse_List[ii]] + 1
    print(parse_Dict)
    print(num_Dict)

    # format the result_Dict
    result_Dict = dict()
    for k in parse_Dict.keys():
        result_Dict[k] = "%d for %s" % (num_Dict[k], parse_Dict[k])

    print(result_Dict)

parse_func(PATH)

代码输出是

['Alpha Beta: Bingo', '2017-06-15', 'Terra Zaba: Bingo', '2017-06-15', 'Terra Zaba: Bingo', '2017-06-15']
{'Alpha Beta: Bingo': '2017-06-15', 'Terra Zaba: Bingo': '2017-06-15'}
{'Alpha Beta: Bingo': 1, 'Terra Zaba: Bingo': 2}
{'Alpha Beta: Bingo': '1 for 2017-06-15', 'Terra Zaba: Bingo': '2 for 2017-06-15'}

写代码的时候我发现一个问题,需要你自己判断。你知道吗

因为dict()中的键需要唯一,当一个元素具有相同的name,但具有不同的time时。如何录制。你知道吗

例如,如果我更改JSON文件。你知道吗

{
  "logs" : [ {
    "other" : "xVKNXCVNsk",
    "time" : "2017-06-15T01:31:50.412Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Alpha Beta: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2017-06-15T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }, {
    "other" : "xVKxXCbNsk",
    "time" : "2016-06-1T01:31:37.229Z",
    "other2" : "xVKxXCbNsk",
    "name" : "Terra Zaba: Bingo"
  }]
}

注意"time" : "2016-06-1T01:31:37.229Z",行已更改。你知道吗

输出将更改为

['Alpha Beta: Bingo', '2017-06-15', 'Terra Zaba: Bingo', '2017-06-15', 'Terra Zaba: Bingo', '2016-06-1']
{'Terra Zaba: Bingo': '2016-06-1', 'Alpha Beta: Bingo': '2017-06-15'}
{'Terra Zaba: Bingo': 2, 'Alpha Beta: Bingo': 1}
{'Terra Zaba: Bingo': '2 for 2016-06-1', 'Alpha Beta: Bingo': '1 for 2017-06-15'}

注意请检查与上面的不同。你知道吗

相关问题 更多 >