【发布时间】:2020-11-21 04:17:00
【问题描述】:
我有一个长字符串列表,我想以特定形式打印输出。 convert list to a particular json in python
但是在数据的转换顺序改变之后。如何保持相同的顺序?
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
....."
]
以特定 json 形式隐藏数据的代码:
input_data = input[0] // input is list of single long string.
input_data = re.split(r',\s*', input_data)
output = collections.defaultdict(collections.Counter)
# print(output)
for line in input_data:
time, error, count = line.split(None, 2)
output[time][error] += int(count)
print(output)
response = [
{
"time": time,
"logs": [
{"exception": exception, "count": count}
for (exception, count) in counter.items()
],
}
for (time, counter) in output.items())
]
print(response)
我的输出:
{
"response": [
{
"logs": [
{
"count": 1,
"exception": "UserNotFoundException"
}
],
"time": "22:45-23:00"
},
{
"logs": [
{
"count": 1,
"exception": "NullPointerException"
}
],
"time": "23:00-23:15"
}...
]
}
所以我的订单发生了变化,但我需要我的数据保持相同的顺序,即从21:15-21:30 开始等等。我怎样才能保持相同的顺序?
【问题讨论】:
-
我不认为它是重复的。这些是列表中的项目,因此它们确实具有定义的顺序。
标签: python arrays json python-3.x list