问题
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 ?
Error :
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
回答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)
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)
outputs (formatted)
[
{
"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,
}
],
},
]
来源:https://stackoverflow.com/questions/63188028/convert-list-to-a-particular-json-in-python