使用python从xml树文件中提取数据

时间:2013-09-05 10:15:33

标签: python xml

我有一个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中的第一个节点,而不是测试名称下的相应部分。有人可以帮忙解决问题吗?

1 个答案:

答案 0 :(得分:0)

这是因为您在循环中使用document.find - 它每次都返回相同的节点。你应该这样:

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