title: How to convert xml into a strucutured csv
body: <p>I have the following xml data and I want to convert it into a csv:</p>
  
  <pre><code>&lt;data&gt;
      &lt;country name="Liechtenstein"&gt;
          &lt;rank updated="yes"&gt;2&lt;/rank&gt;
          &lt;year&gt;2008&lt;/year&gt;
          &lt;gdppc&gt;141100&lt;/gdppc&gt;
          &lt;neighbor name="Austria" direction="E"/&gt;
          &lt;neighbor name="Switzerland" direction="W"/&gt;
      &lt;/country&gt;
      &lt;country2 name="Singapore"&gt;
          &lt;rank updated="yes"&gt;5&lt;/rank&gt;
          &lt;year&gt;2011&lt;/year&gt;
          &lt;gdppc&gt;59900&lt;/gdppc&gt;
          &lt;neighbor name="Malaysia" direction="N"/&gt;
      &lt;/country2&gt;
      &lt;country3 name="Panama"&gt;
          &lt;rank updated="yes"&gt;69&lt;/rank&gt;
          &lt;year&gt;2011&lt;/year&gt;
          &lt;gdppc&gt;13600&lt;/gdppc&gt;
          &lt;neighbor name="Costa Rica" direction="W"/&gt;
          &lt;neighbor name="Colombia" direction="E"/&gt;
      &lt;/country3&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>How can I get each country line represented as 1 row in csv with all the attributes inside each country. I can't figure out how to read multiple countries. </p>
  
  <p>I have the following code:</p>
  
  <pre><code>import os
  from xml.etree import ElementTree as ET
  
  
  
  rootElement = ET.parse("/Users/testuser/Desktop/test.XML").getroot()
  
  
  with open('/Users/testuser/Desktop/output.csv', 'wb') as csvfile:
      writer = csv.writer(csvfile, lineterminator='\n')
  
  
      for subelement in rootElement:
          for subsub in subelement:
              print subsub.tag
              writer.writerows(subsub.items())
              for subsubsub in subsub:
                  print subsubsub.items()
                  writer.writerows(subsubsub.items())
  </code></pre>
  
  <p>This doesn't give me the desired output unfortunatately. In my CSV I want each row to represent a country. So row 1 would be country, row 2 would be country2. And then each column should give me the value of the subelements. So in my csv A1 would be country/name, A2 would be country/rank and so on. B1 would be country2/name, B2 would be country2/rank..</p>
  
  <p>currently I get the following output:</p>
  
  <p><a href="https://i.stack.imgur.com/150ba.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/150ba.png" alt="enter image description here"></a></p>