0

下面是解析下面发布的 xml 的脚本/函数。该代码在 python3 上运行良好,当我移植到 2.6 ts 时出现以下错误

这里有什么问题?

Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "D:\Squish\Run_Test2.6.py", line 214, in sel_submit
    xml_parser()
  File "D:\Squish\Run_Test2.6.py", line 59, in xml_parser
    for test in tree.findall('.//test[verification]'):
  File "C:\Python26\lib\xml\etree\ElementTree.py", line 647, in findall
    return self._root.findall(path)
  File "C:\Python26\lib\xml\etree\ElementTree.py", line 355, in findall
    return ElementPath.findall(self, path)
  File "C:\Python26\lib\xml\etree\ElementPath.py", line 198, in findall
    return _compile(path).findall(element)
  File "C:\Python26\lib\xml\etree\ElementPath.py", line 176, in _compile
    p = Path(path)
  File "C:\Python26\lib\xml\etree\ElementPath.py", line 93, in __init__
    "expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator ([)

下面是代码

def xml_parser():
    # global variables
    global text_file
    global xml_list
    # File names
    xml_list = glob.glob("%s*.xml" % (os.path.join(SUITE_DIR +"/")))
    text_file = (os.path.join(SUITE_DIR)+"/Result_Summary-%s.txt"% (time.strftime("%Y-%m-%d")))
    with open("%s"%text_file,"w") as output:
            # Write Contents in the summary files
            output.write(' {} \n\n'.format('-' * 122))
            output.write('  Test Suite \t\t Test Name \t\t No Of PASS\t\t No Of FAIL\t\t Description\t\t \n')
            output.write(' {} \n\n'.format('-' * 122))
    for xml_name in xml_list:
        tree = ET.parse(xml_name)
        root = tree.getroot()
        with open("%s"%text_file,"a") as output:
            # Find all <test> elements with a <verification> child:
            for test in tree.findall('.//test[verification]'):
                # Collect passed and failed counts
                sut=tree.find('./test')
                passed = len(test.findall(".//result[@type='PASS']"))
                failed = len(test.findall(".//result[@type='FAIL']"))
                # Collect all the *last* <description> elements of type DETAILED
                descriptions = test.findall(".//result/description[@type='DETAILED'][last()]")
                # write a line of information to the file, including first desc
                output.write('{0}\t\t\t{1}\t\t\t{2}\t\t\t{3}\t\t{4}\n\n'.format(
                    sut.attrib['name'],test.attrib['name'], passed, failed, descriptions[0].text))
                # write remaining descriptions
                for desc in descriptions[1:]:
                    output.write('\t\t\t\t\t\t\t\t\t\t\t{0}\n'.format(desc.text))

下面是xml格式

   <SquishReport version="2.0">
    <test name="HMI_testing_debug">
        <prolog time="2013-01-25T16:51:58+05:30"/>
        <test name="tst_Setup_me">
            <prolog time="2013-01-25T16:51:58+05:30"/>
            <verification line="7" file="D:/Squish/HMI_testing_debug/tst_Setup_me/test.py" name="Is TEMP is disabled">
                <result type="PASS" time="2013-01-25T16:52:00+05:30">
                    <description>Comparison</description>
                    <description type="DETAILED">'0' and 'False' are equal</description>
                    <description type="DETAILED">Is TEMP is disabled</description>
                </result>
            </verification>
            <message line="9" type="ERROR" file="D:/Squish/HMI_testing_debug/tst_Setup_me/test.py" time="2013-01-25T16:52:21+05:30"><![CDATA[LookupError: Object ':_QMenu' not found. Could not match properties:
    type for object name: ':_QMenu']]></message>
            <epilog time="2013-01-25T16:52:21+05:30"/>
        </test>
        <epilog time="2013-01-25T16:52:21+05:30"/>
    </test>
</SquishReport>

我已经用下面的代码解决了上述问题,我无法将下面的代码作为答案。如果有人知道更好的方法来解决同样的问题,请告诉我

def xml_parser():
    # global variables
    global text_file
    global xml_list
    # File names
    xml_list = glob.glob("%s*.xml" % (os.path.join(SUITE_DIR +"/")))
    text_file = (os.path.join(SUITE_DIR)+"/Result_Summary-%s.txt"% (time.strftime("%Y-%m-%d")))
    with open("%s"%text_file,"w") as output:
            # Write Contents in the summary files
            output.write(format('-' * 122))
            output.write('  \nTest Suite \t\t Test Name \t\t No Of PASS\t\t No Of FAIL\t\t Description\t\t \n')
            output.write(format('-' * 122))
    for xml_name in xml_list:
        print(xml_name)
        tree = ET.parse(xml_name)
        root = tree.getroot()
        with open("%s"%text_file,"a") as output:
            for suite_x in tree.findall('./test'):
                output.write('\n')
                print(suite_x.attrib['name'])
                output.write(suite_x.attrib['name'])
                output.write('\t\t')
                for test_x in suite_x.findall('.//test'):
                    nextline = 0
                    output.write(test_x.attrib['name'])
                    output.write('\t\t')
                    print(test_x.attrib['name'])                
                    for veri_x in test_x.findall('./verification'):
                        passed = 0
                        failed = 0
                        for count in veri_x:
                            if(nextline):
                                output.write('\n')
                                output.write('\t\t\t\t\t\t')
                            if( count.attrib['type']=='PASS'):
                                passed=passed+1
                            elif( count.attrib['type']=='FAIL'):
                                failed=failed+1
                            output.write(str(passed))
                            output.write('\t\t')
                            output.write(str(failed))
                            output.write('\t\t')
                            print("passed: %s" %passed,"failed: %s" %failed)
                            print(veri_x.attrib['name'])
                            output.write(veri_x.attrib['name'])
                            nextline = 1
4

1 回答 1

0

Python 2.6 不支持 XPath(它是在2.7附带 的ElementTree 1.3中引入的。

因此,您应该将 Python 2 升级到 2.7,或者修改代码以避免使用 XPath。

于 2013-04-12T06:57:45.640 回答