【发布时间】: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