【问题标题】:convert list to a particular json in python在python中将列表转换为特定的json
【发布时间】:2020-11-21 01:49:11
【问题描述】:

我正在解决一个问题,我的输出是这样的: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
  ....."]

我需要像这样转换数据:

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

如何将数据转换成这种特定格式?

错误:

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

【问题讨论】:

  • Python 有一个 json 库可以将列表转换为 json,但是要归档您发布的这种特定格式,您必须事先解析您的列表,例如使用正则表达式。

标签: python arrays json python-3.x list


【解决方案1】:

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

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 也是)派上用场:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2020-06-03
  • 2014-08-27
相关资源
最近更新 更多