【问题标题】:Can we use if statement with Etree Parsing我们可以在 Etree Parsing 中使用 if 语句吗
【发布时间】: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 个描述结果,打印出每个唯一字段的文本(对于“失败”,文本将来自 &lt;VerificationFailed&gt;,对于“不完整”,文本将来自 &lt;TestDescription&gt;,对于“失败” , Incomplete, Error”文本将来自&lt;ExceptionThrown&gt;,最后对于“Passed”,它将是字符串“Passed”。

我已经尝试过代码,但它只给出了传递的值。我需要它能够获得不同的值。

【问题讨论】:

  • 修复 XML 并解释预期输出是什么
  • XML 文件已编辑,我也尽力解释预期的输出(抱歉我的英语不好):)

标签: python xml elementtree


【解决方案1】:

我认为以下是您正在寻找的内容

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.

【讨论】:

  • 您更改了 XML。部分问题在于原始 XML 中有很多空格。
  • @mzjn 我已将 xml 复制到在线 xml 格式化程序 freeformatter.com/xml-formatter.html 中,我正在处理结果。我不知道进行了任何重大更改..
  • 例如,在执行if result == 'Failed' 时很重要。 “失败”在问题的 XML 中被空格包围。
  • 知道了。所以strip() 应该关心它 - 不是吗?
  • @mzjn 代码已修改以支持 original xml
猜你喜欢
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2011-08-06
  • 1970-01-01
  • 2015-11-07
  • 2020-11-09
相关资源
最近更新 更多