【发布时间】:2022-01-11 11:34:17
【问题描述】:
我很困惑,想问一下find()方法在循环中使用的if语句。
我想将这些数据插入数据库,每个数据都有一个唯一值,只能通过其结果来识别。
可用的结果是“通过”//“不完整”//“失败”//“失败、不完整、错误”//
这是文件的示例
<TestSuite>
<TestCase>
<TestResult>
Failed
</TestResult>
<VerificationFailed>
Not enough Fuel.
</VerificationFailed>
</TestCase>
<TestCase>
<TestResult>
Passed
</TestResult>
</TestCase>
<TestCase>
<TestResult>
Incomplete
</TestResult>
<TestDescription>
Engine not set up properly.
</TestDescription>
</TestCase>
<TestCase>
<TestResult>
Failed, Incomplete, Error
</TestResult>
<ExceptionThrown>
Error, Capacity Overload.
</ExceptionThrown>
</TestCase>
</TestSuite>
我已尝试使用此代码获取这些字段的值,但它只返回“通过”
tree = ET.parse('NewestReport.xml')
test = tree.findall('TestCase')
for ts in test:
result = ts.find('TestResult').text
if result in "Failed":
description = ts.find('VerificationFailed').text
print(description)
elif result in "Failed, Incomplete, Error":
description = ts.find('ExceptionThrown').text
print(description)
elif result in "Incomplete":
description = ts.find('TestDescription').text
print(description)
else:
description = "Passed"
print(description)
预期的输出是 4 个描述结果,打印出每个唯一字段的文本(对于“失败”,文本将来自 <VerificationFailed>,对于“不完整”,文本将来自 <TestDescription>,对于“失败” , Incomplete, Error”文本将来自<ExceptionThrown>,最后对于“Passed”,它将是字符串“Passed”。
我已经尝试过代码,但它只给出了传递的值。我需要它能够获得不同的值。
【问题讨论】:
-
修复 XML 并解释预期输出是什么
-
XML 文件已编辑,我也尽力解释预期的输出(抱歉我的英语不好):)
标签: python xml elementtree