我们可以在 Etree 解析中使用 if 语句吗
Can we use if statement with Etree Parsing
我很困惑,想问一下 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>
我曾尝试使用此代码获取这些字段的值,但它仅 returns“通过”
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>
,对于“失败、不完整、错误”,文本将来自 <ExceptionThrown>
,最后对于“已通过”,它将是字符串“已通过”。
我试过代码,但它只给出传递的值。我需要它才能获得不同的值。
我想下面就是你要找的东西
import xml.etree.ElementTree as ET
xml = '''<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>'''
root = ET.fromstring(xml)
for tc in root.findall('.//TestCase'):
result = tc.find('TestResult').text.strip()
if result == 'Failed':
print(f'{result} : {tc.find("VerificationFailed").text.strip()}')
elif result == 'Passed':
print(result)
elif result == 'Incomplete':
print(f'{result} : {tc.find("TestDescription").text.strip()}')
elif result == 'Failed, Incomplete, Error':
print(f'{result} : {tc.find("ExceptionThrown").text.strip()}')
输出
Failed : Not enough Fuel.
Passed
Incomplete : Engine not set up properly.
Failed, Incomplete, Error : Error, Capacity Overload.
我很困惑,想问一下 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>
我曾尝试使用此代码获取这些字段的值,但它仅 returns“通过”
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>
,对于“失败、不完整、错误”,文本将来自 <ExceptionThrown>
,最后对于“已通过”,它将是字符串“已通过”。
我试过代码,但它只给出传递的值。我需要它才能获得不同的值。
我想下面就是你要找的东西
import xml.etree.ElementTree as ET
xml = '''<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>'''
root = ET.fromstring(xml)
for tc in root.findall('.//TestCase'):
result = tc.find('TestResult').text.strip()
if result == 'Failed':
print(f'{result} : {tc.find("VerificationFailed").text.strip()}')
elif result == 'Passed':
print(result)
elif result == 'Incomplete':
print(f'{result} : {tc.find("TestDescription").text.strip()}')
elif result == 'Failed, Incomplete, Error':
print(f'{result} : {tc.find("ExceptionThrown").text.strip()}')
输出
Failed : Not enough Fuel.
Passed
Incomplete : Engine not set up properly.
Failed, Incomplete, Error : Error, Capacity Overload.