将列表转换为python中的特定json [英] convert list to a particular json in python

查看:72
本文介绍了将列表转换为python中的特定json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个问题,我的输出是这样的:其中包含长字符串的列表

I am working on a problem and my output is something like this: List with long string inside it

["21:15-21:30 IllegalAgrumentsException 1,
  21:15-21:30 NullPointerException 2,
  22:00-22:15 UserNotFoundException 1,
  22:15-22:30 NullPointerException 1
  ....."]

我需要将数据转换为以下形式:

I need to convert the data in something like this:

response: [
     {
        "time": "21:15-21:30",
        "logs": [
            {
                "exception": "IllegalAgrumentsException",
                "count": 1
            },{
                "exception": "NullPointerException",
                "count": 2
            }
        ]
    },
    {
        "time": "22:00-22:15",
        "logs": [
            {.....
            }
         ]
    }
]

如何将数据转换为这种特殊格式吗?

How can I convert the data into this particular format ?

错误:

print(type(input)) // <class 'list'>
input = re.split(r',\s+', input[0])
print(input)  // ['21:15-21:30 IllegalAgrumentsException 1,
                   21:15-21:30 NullPointerException 2.....']

output = collections.defaultdict(collections.Counter)
for line in input:
    time, error, count = line.split(None, 2)
    output[time][error] += int(count)  //invalid literal for int() with base 10: '1


推荐答案

我假设您的输入列表实际上是一个字符串列表,但如果不是,则表示可以先分割字符串:

I'm assuming your input list is actually a list of strings, but if it's not, you can split the string first:

import re
# If the input data is a list that has one entry, peel it off
input_data = input_data[0]
# Now we should have a string to split...
input_data = re.split(r',\s*', input_data)

对于分组和整理操作,通常使用 collections.defaultdict (以及在这种特殊的求和情况下, collections.Counter 也是如此)

As usual for a group-and-collate operation, collections.defaultdict (and in this particular summing case, collections.Counter too) come in handy:

import collections

input_data = [
    "21:15-21:30 IllegalAgrumentsException 1",
    "21:15-21:30 NullPointerException 2",
    "22:00-22:15 UserNotFoundException 1",
    "22:15-22:30 NullPointerException 1",
]

output = collections.defaultdict(collections.Counter)
for line in input_data:
    time, error, count = line.split(None, 2)
    output[time][error] += int(count)

response = [
    {
        "time": time,
        "logs": [
            {"exception": exception, "count": count}
            for (exception, count) in counter.items()
        ],
    }
    for (time, counter) in output.items()
]

print(response)

输出(格式化的)

[
    {
        "time": "21:15-21:30",
        "logs": [
            {
                "exception": "IllegalAgrumentsException",
                "count": 1,
            },
            {
                "exception": "NullPointerException",
                "count": 2,
            },
        ],
    },
    {
        "time": "22:00-22:15",
        "logs": [
            {
                "exception": "UserNotFoundException",
                "count": 1,
            }
        ],
    },
    {
        "time": "22:15-22:30",
        "logs": [
            {
                "exception": "NullPointerException",
                "count": 1,
            }
        ],
    },
]

这篇关于将列表转换为python中的特定json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆