title: Issues indexing data extract from xml
body: <p>I'm working with some XML data that will ultimately be loaded into a csv.  I am experiencing an issue with properly indexing the data when a element doesn't exist in an entry.  Below is a simple xml example of what I am working with</p>
  <pre><code>&lt;root&gt;
      &lt;entry&gt;
          &lt;LASTNAME&gt;Doe&lt;/LASTNAME&gt;
          &lt;FIRSTNAME&gt;Jon&lt;/FIRSTNAME&gt;
          &lt;GENDER&gt;M&lt;/GENDER&gt;
      &lt;/entry&gt;
      &lt;entry&gt;
          &lt;LASTNAME&gt;Doe&lt;/LASTNAME&gt;
          &lt;FIRSTNAME&gt;Jane&lt;/FIRSTNAME&gt;
          &lt;GENDER&gt;F&lt;/GENDER&gt;
          &lt;HAIRCOLOR&gt;Blonde&lt;/HAIRCOLOR&gt;
      &lt;/entry&gt;
  &lt;/root&gt;
  </code></pre>
  <p>The output I end up getting is as follows:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>LASTNAME</th>
  <th>FIRSTNAME</th>
  <th>GENDER</th>
  <th>HAIRCOLOR</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Doe</td>
  <td>John</td>
  <td>M</td>
  <td>Blonde</td>
  </tr>
  <tr>
  <td>Doe</td>
  <td>Jane</td>
  <td>F</td>
  <td></td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>But the correct output should be:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>LASTNAME</th>
  <th>FIRSTNAME</th>
  <th>GENDER</th>
  <th>HAIRCOLOR</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Doe</td>
  <td>John</td>
  <td>M</td>
  <td></td>
  </tr>
  <tr>
  <td>Doe</td>
  <td>Jane</td>
  <td>F</td>
  <td>Blonde</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>So I seem to have an indexing problem where the first few times HAIRCOLOR (depending on the number of HAIRCOLOR elements are present on the page) is searched for, it goes down the XML until it finds one, but it should stop when it reaches the end of the entry.</p>
  <p>Here's the code I am working with:</p>
  <pre><code>import xml.etree.ElementTree as ET
  from xml.etree.ElementTree import ParseError
  from xml.etree import ElementTree
  
  
  bytes_ = '''
  &lt;root&gt;
      &lt;entry&gt;
          &lt;LASTNAME&gt;Doe&lt;/LASTNAME&gt;
          &lt;FIRSTNAME&gt;Jon&lt;/FIRSTNAME&gt;
          &lt;GENDER&gt;M&lt;/GENDER&gt;
      &lt;/entry&gt;
      &lt;entry&gt;
          &lt;LASTNAME&gt;Doe&lt;/LASTNAME&gt;
          &lt;FIRSTNAME&gt;Jane&lt;/FIRSTNAME&gt;
          &lt;GENDER&gt;F&lt;/GENDER&gt;
          &lt;HAIRCOLOR&gt;Blonde&lt;/HAIRCOLOR&gt;
      &lt;/entry&gt;
  &lt;/root&gt;
  '''
  xpaths = [
      &quot;./entry/LASTNAME&quot;,
      &quot;./entry/FIRSTNAME&quot;,
      &quot;./entry/GENDER&quot;,
      &quot;./entry/HAIRCOLOR&quot;
  ]
  
  data = []
  
  _fields = [
          {'text' : ''},
          {'text' : ''},
          {'text' : ''},
          {'text' : ''}
          ]
  
  root = ET.fromstring(bytes_)
  
  
      for count in range(0,len(root.findall(&quot;./entry&quot;))):
          
          for ele, xpath in enumerate(xpaths):
              try:
                  attribs = list(root.findall(xpath)[count].attrib.keys())
                  
                  for attrib in attribs:
                      for i in _fields[ele].keys():
                  
                          if attrib == i:
                              _fields[ele][i] = root.findall(xpath)[count].attrib[attrib]
                  _fields[ele][&quot;text&quot;] =root.findall(xpath)[count].text
                  
  
              except IndexError:
                  _fields[ele][&quot;text&quot;]=''
  
              data.append(_fields[ele].values())
              data_list = [item for sublist in data for item in sublist]
          data.clear()
          print(data_list)
  
  </code></pre>
  <p>Any help is appreciated.</p>
  <p>Edited for clarity</p>