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 に答える