1

我有一个 XML 文件,其中有一些我想提取并写入 xls 文件的测试限制。

<TestLimitSet>
<AppName>Test Application</AppName>
<Name>High-Speed test app</Name>
<SummaryName>limit set 1.0</SummaryName>
<SharedReference>DUT Specification Version 1.0</SharedReference>
<TestLimits>
    <!--########## Host Electrical Specification ##########-->
    <!--Host Strobe Frequency-->
    <TestLimit>
        <ID>1101, 1102</ID>
        <SummaryName>Host Strobe Frequency</SummaryName>
        <SummaryDescription>The frequency must within the specification.</SummaryDescription>
        <Reference>Section 3.</Reference>
        <PrecisionLevel>0.01E+6</PrecisionLevel>
        <PassLimits>
            <Type>RANGE_INCLUSIVE</Type>
            <Min>239.88E+6</Min>
            <Max>240.12E+6</Max>
        </PassLimits>
    </TestLimit>

    <!--Host Data Slew Rate-->
    <TestLimit>
        <ID>1201</ID>
        <SummaryName>Host Data Slew Rate</SummaryName>
        <SummaryDescription>Slew rate must within the specification.</SummaryDescription>
        <Reference>Section 4.</Reference>
        <PrecisionLevel>0.001</PrecisionLevel>
        <PassLimits>
            <Type>RANGE_INCLUSIVE</Type>
            <Min>0.7</Min>
            <Max>2.0</Max>
        </PassLimits>
    </TestLimit>

    <!--Host Strobe Slew Rate-->
    <TestLimit>
        <ID>1202</ID>
        <SummaryName>Host Strobe Slew Rate</SummaryName>
        <SummaryDescription>Slew rate must within the specification.</SummaryDescription>
        <Reference>Section 5.</Reference>
        <PrecisionLevel>0.001</PrecisionLevel>
        <PassLimits>
            <Type>RANGE_INCLUSIVE</Type>
            <Min>0.7</Min>
        </PassLimits>
    </TestLimit>

</TestLimits>

我使用以下 python 代码来提取数据:

from xml.etree import ElementTree
document = ElementTree.parse( 'limitset.lim' )
#
for testname in document.findall( 'TestLimits/TestLimit/SummaryName' ):
    print 'TestName:', testname.text
    limits = document.find( 'TestLimits/TestLimit/PassLimits' )
    for node in limits.getchildren():
        print node.tag, node.text

这是输出:

TestName: Host Strobe Frequency
Type RANGE_INCLUSIVE
Min 239.88E+6
Max 240.12E+6
TestName: Host Data Slew Rate
Type RANGE_INCLUSIVE
Min 239.88E+6
Max 240.12E+6
TestName: Host Strobe Slew Rate
Type RANGE_INCLUSIVE
Min 239.88E+6
Max 240.12E+6

问题是“Min”和“Max”总是来自 XML 中的第一个节点,而不是测试名称下的相应部分。有人可以帮助解决问题吗?

4

1 回答 1

0

This is because you are using document.find in the loop - it returns the same node each time. You should go this way:

for limit in document.findall('TestLimits/TestLimit'):
    testname = limit.find('.//SummaryName')
    print 'TestName:', testname.text
    limits = limit.find('.//PassLimits')
    for node in limits.getchildren():
        print node.tag, node.text
于 2013-09-05T10:18:53.110 回答