title: Python ElementTree xml output to csv
body: <p>I have the following XML file ('registerreads_EE.xml'):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="us-ascii" standalone="yes"?&gt;
  &lt;ReadingDocument xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;ReadingStatusRefTable&gt;
  &lt;ReadingStatusRef Ref="1"&gt;
    &lt;UnencodedStatus SourceValidation="SIMPLE"&gt;
      &lt;StatusCodes&gt;
        &lt;Signal&gt;XX&lt;/Signal&gt;
      &lt;/StatusCodes&gt;
    &lt;/UnencodedStatus&gt;
  &lt;/ReadingStatusRef&gt;
    &lt;/ReadingStatusRefTable&gt;
    &lt;Header&gt;
  &lt;IEE_System Id="XXXXXXXXXXXXXXX" /&gt;
  &lt;Creation_Datetime Datetime="2015-10-22T09:05:32Z" /&gt;
  &lt;Timezone Id="UTC" /&gt;
  &lt;Path FilePath="X:\XXXXXXXXXXXX.xml" /&gt;
  &lt;Export_Template Id="XXXXX" /&gt;
  &lt;CorrelationID Id="" /&gt;
    &lt;/Header&gt;
    &lt;ImportExportParameters ResubmitFile="false" CreateGroup="true"&gt;
      &lt;DataFormat TimestampType="XXXXXX" Type="XXXX" /&gt;
    &lt;/ImportExportParameters&gt;
    &lt;Channels&gt;
  &lt;Channel StartDate="2015-10-21T00:00:00-05:00" EndDate="2015-10-22T00:00:00-05:00"&gt;
    &lt;ChannelID ServicePointChannelID="73825603:301" /&gt;
    &lt;Readings&gt;
      &lt;Reading Value="3577.0" ReadingTime="2015-10-21T00:00:00-05:00" StatusRef="1" /&gt;
      &lt;Reading Value="3601.3" ReadingTime="2015-10-22T00:00:00-05:00" StatusRef="1" /&gt;
    &lt;/Readings&gt;
    &lt;ExportRequest RequestID="152" EntityType="ServicePoint" EntityID="73825603" RequestSource="Scheduled" /&gt;
  &lt;/Channel&gt;
      &lt;Channel StartDate="2015-10-21T00:00:00-05:00" EndDate="2015-10-22T00:00:00-05:00"&gt;
    &lt;ChannelID ServicePointChannelID="73825604:301" /&gt;
    &lt;Readings&gt;
      &lt;Reading Value="3462.5" ReadingTime="2015-10-21T00:00:00-05:00" StatusRef="1" /&gt;
      &lt;Reading Value="3501.5" ReadingTime="2015-10-22T00:00:00-05:00" StatusRef="1" /&gt;
    &lt;/Readings&gt;
    &lt;ExportRequest RequestID="152" EntityType="ServicePoint" EntityID="73825604" RequestSource="Scheduled" /&gt;
  &lt;/Channel&gt;
    &lt;/Channels&gt;
  &lt;/ReadingDocument&gt;
  </code></pre>
  
  <p>I want to parse the XML of the channel data into a csv file.</p>
  
  <p>He is what I have written in Python 2.7.10:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  
  tree = ET.parse('registerreads_EE.xml')
  
  root = tree.getroot()[3]
  
  for channel in tree.iter('Channel'):
      for exportrequest in channel.iter('ExportRequest'):
          entityid = exportrequest.attrib.get('EntityID')
          for meterread in channel.iter('Reading'):
              read = meterread.attrib.get('Value')
              date = meterread.attrib.get('ReadingTime')
              print read[:-2],",",date[:10],",",entityid
  
  tree.write(open('registerreads_EE.csv','w'))
  </code></pre>
  
  <p>Here is the screen output when the above is run:</p>
  
  <pre><code>3577 , 2015-10-21 , 73825603
  3601 , 2015-10-22 , 73825603
  3462 , 2015-10-21 , 73825604
  3501 , 2015-10-22 , 73825604
  </code></pre>
  
  <p>The 'registerreads.csv' output file is like the original XML file, minus the first line.</p>
  
  <p>I would like the printed output above outputted to a csv file with headers of read, date, entityid.</p>
  
  <p>I am having difficulty with this.  This is my first python program.  Any help is appreciated. </p>