【发布时间】:2020-03-01 08:06:43
【问题描述】:
我有一个测试失败列表,如下所示 -
all_failures = [
'test1/path/to/test1/log/failure_reason1',
'test1/path/to/test1/log/failure_reason2',
'test2/path/to/test2/log/failure_reason1',
'test2/path/to/test2/log/failure_reason2',
'test3/path/to/test3/log/failure_reason1',
'test4/path/to/test4/log/failure_reason1',
]
我试图通过解析列表中的每个失败来构造一个类似 JSON 的对象。到目前为止,我已经尝试编写以下代码 -
for failure in all_failures:
data = failure.split('/',1)
test = data[0]
failure_details_dict[test] = []
data = '/' + data[1]
data = data.rsplit('/', 1)
test_details_dict['path'] = data[0] + '/'
test_details_dict['reason'] = data[1]
failure_details_dict[test].append(test_details_dict)
test_details_dict = {}
for key,value in failure_details_dict.items():
print(key)
print(value)
print()
我得到的输出是 -
test4
[{'reason': 'failure_reason1', 'path': '/path/to/test4/log/'}]
test3
[{'reason': 'failure_reason1', 'path': '/path/to/test3/log/'}]
test1
[{'reason': 'failure_reason2', 'path': '/path/to/test1/log/'}]
test2
[{'reason': 'failure_reason2', 'path': '/path/to/test2/log/'}]
然而,预期输出是 -
{
"test1": [
{
"path": "/path/to/test1/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test1/log/",
"reason": "failure_reason2"
}
],
"test2": [
{
"path": "/path/to/test2/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test2/log/",
"reason": "failure_reason2"
}
],
"test3": [
{
"path": "/path/to/test3/log/",
"reason": "failure_reason1"
},
],
"test4": [
{
"path": "/path/to/test4/log/",
"reason": "reason1"
},
]
}
正如我们所见,我无法将 second path 和 reason for failure 添加到同一个键中。示例 - test1 和 test2 有两个失败的原因。
有人可以帮助了解我缺少什么吗?谢谢!
【问题讨论】: