我有一个测试失败列表,如下所示-
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"
},
]
}
如我们所见,我无法将第二个路径和失败原因添加到同一个键中。示例-test1和test2有两个失败原因
有人能帮我理解我遗漏了什么吗?谢谢大家!
您可以使用正则表达式从故障日志文件名中提取信息。这可以通过以下方式简单实现:
输出:
理由
对于每个循环,您都将覆盖到
failure_details_dict[test]
解决方案
您应该只将列表设置为一次。
您有多种选择
dict.setdefault
调用。这种方式不会影响与failure_details_dict
的其他交互collections.defaultdict
代替dict
。这种方式将影响与failure_detilas_dict
的其他交互李>范例
我已经重构了你的代码:
结论
dict.setdefault
方法李>failure_details_dict
有多个访问,并且希望每个访问都有默认值,请使用collection.defaultdict
类李>额外的
您可以重新格式化JSON,如:
从代码:
相关问题 更多 >
编程相关推荐