Parsing XML with throws unknown key error in function but works at command prompt
<p>I'm running Python 2.7.12 in Anaconda 4.1.1. I installed untangle to parse a pretty complex XML document.</p>

<p>Here's my code:</p>

<pre><code>import untangle

obj = untangle.parse('ear.xml')

for rd in obj.SaData.Session.Test.Data.RecordedData:
    tls = rd.Measured.TestLines
    tl = tls.Testline
    for line in tl:
        snl = line.SnLevel.cdata
        pn = line.PresentNoise.cdata
        print snl + " " + pn   
</code></pre>

<p>This returns the following error message:</p>

<p>IndexError: Unknown key </p>

<p>But if I immediately run tl = tls.Testline from the command prompt, I don't get any error.</p>

<p>Gotta be something simple but I'm a noob so help appreciated.</p>

<p>EDIT: I can't attach a file and the fully expanded XML is too big to enter here. I will try to present a partially expanded version to give some sense of how the file is organized.</p>

<pre><code>&lt;SaData Version="2" xsi:schemaLocation="uuid:ee2fbfd9-47a5-4dc8-a9eb-42d9995802ab SaData.xsd"&gt;
&lt;ClientInfo&gt;&lt;/ClientInfo&gt;
&lt;Session&gt;&lt;Platform FirmwareVersion=""&gt;&lt;/Platform&gt;&lt;Created&gt;2016-09-21T11:08:58&lt;/Created&gt;
&lt;Changed&gt;2016-09-21T11:08:58&lt;/Changed&gt;
&lt;Module Version="2.0.0.0"&gt;DPOAE&lt;/Module&gt;
&lt;ProtocolName&gt;DP 2 - 10 kHz (8/octave)&lt;/ProtocolName&gt;
&lt;Settings&gt;&lt;/Settings&gt;
&lt;Test&gt;&lt;TestName&gt;DP-Gram&lt;/TestName&gt;
&lt;Settings&gt;&lt;/Settings&gt;
&lt;Data&gt;
&lt;RecordedData&gt;
&lt;Settings&gt;&lt;/Settings&gt;
&lt;Measured&gt;
&lt;Earside&gt;Left&lt;/Earside&gt;
&lt;TestType&gt;DPGram&lt;/TestType&gt;
&lt;Readonly&gt;false&lt;/Readonly&gt;
&lt;PeakPressure&gt;-5&lt;/PeakPressure&gt;
&lt;TestStatus&gt;9&lt;/TestStatus&gt;
&lt;TestLines&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;TestLine&gt;&lt;/TestLine&gt;
&lt;/TestLines&gt;
&lt;TimeStamp&gt;2016-09-19T12:28:11.7110965-05:00&lt;/TimeStamp&gt;&lt;Duration&gt;PT1M30S&lt;/Duration&gt;
&lt;/Measured&gt;
&lt;Calculated&gt;&lt;/Calculated&gt;
&lt;PrivateData&gt;&lt;/PrivateData&gt;
&lt;/RecordedData&gt;&lt;
RecordedData&gt;&lt;/RecordedData&gt;
&lt;/Data&gt;
&lt;/Test&gt;
&lt;/Session&gt;&lt;/SaData&gt;
</code></pre>

<p>It was a stupid typo. I wrote Testline when I should have written TestLine. Sorry to waste everyone's time.</p>

<p>Dessie</p>

python xml