title: XML to CSV in Python
body: <p>I'm having a lot of trouble converting an XML file to a CSV in Python. I've looked at many forums, tried both lxml and xmlutils.xml2csv, but I can't get it to work. It's GPS data from a Garmin GPS device.</p>
  
  <p>Here's what my XML file looks like, shortened of course:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;gpx xmlns:tc2="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tp1="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="TC2 to GPX11 XSLT stylesheet" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"&gt;
    &lt;trk&gt;
        &lt;name&gt;2013-12-03T21:08:56Z&lt;/name&gt;
        &lt;trkseg&gt;
            &lt;trkpt lat="45.4852855" lon="-122.6347885"&gt;
                &lt;ele&gt;0.0000000&lt;/ele&gt;
                &lt;time&gt;2013-12-03T21:08:56Z&lt;/time&gt;
            &lt;/trkpt&gt;
            &lt;trkpt lat="45.4852961" lon="-122.6347926"&gt;
                &lt;ele&gt;0.0000000&lt;/ele&gt;
                &lt;time&gt;2013-12-03T21:09:00Z&lt;/time&gt;
            &lt;/trkpt&gt;
            &lt;trkpt lat="45.4852982" lon="-122.6347897"&gt;
                &lt;ele&gt;0.2000000&lt;/ele&gt;
                &lt;time&gt;2013-12-03T21:09:01Z&lt;/time&gt;
            &lt;/trkpt&gt;
        &lt;/trkseg&gt;
    &lt;/trk&gt;
  &lt;/gpx&gt;
  </code></pre>
  
  <p>There are several trk tags in my massive XML file, but I can manage to separate them out -- they represent different "segments" or trips on the GPS device. All I want is a CSV file that plots something like this:</p>
  
  <pre><code>LAT         LON         TIME         ELE
  45.4...     -122.6...   2013-12...   0.00...
  ...         ...         ...          ...
  </code></pre>
  
  <p>Here's the code I have so far: </p>
  
  <pre><code>## Call libraries
  import csv
  from xmlutils.xml2csv import xml2csv
  
  inputs = "myfile.xml"
  output = "myfile.csv"
  
  converter = xml2csv(inputs, output)
  converter.convert(tag="WHATEVER_GOES_HERE_RENDERS_EMPTY_CSV")
  </code></pre>
  
  <p>This is another alternative code. It merely outputs a CSV file with no data, just the headers <code>lat</code> and <code>lon</code>.</p>
  
  <pre><code>import csv
  import lxml.etree
  
  x = '''
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;gpx xmlns:tc2="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tp1="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="TC2 to GPX11 XSLT stylesheet" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"&gt;
  &lt;trk&gt;
    &lt;name&gt;2013-12-03T21:08:56Z&lt;/name&gt;
    &lt;trkseg&gt;
      &lt;trkpt lat="45.4852855" lon="-122.6347885"&gt;
        &lt;ele&gt;0.0000000&lt;/ele&gt;
        &lt;time&gt;2013-12-03T21:08:56Z&lt;/time&gt;
      &lt;/trkpt&gt;
      &lt;trkpt lat="45.4852961" lon="-122.6347926"&gt;
        &lt;ele&gt;0.0000000&lt;/ele&gt;
        &lt;time&gt;2013-12-03T21:09:00Z&lt;/time&gt;
      &lt;/trkpt&gt;
      &lt;trkpt lat="45.4852982" lon="-122.6347897"&gt;
        &lt;ele&gt;0.2000000&lt;/ele&gt;
        &lt;time&gt;2013-12-03T21:09:01Z&lt;/time&gt;
      &lt;/trkpt&gt;
    &lt;/trkseg&gt;
  &lt;/trk&gt;
  &lt;/gpx&gt;
  '''
  
  with open('output.csv', 'w') as f:
      writer = csv.writer(f)
      writer.writerow(('lat', 'lon'))
      root = lxml.etree.fromstring(x)
      for trkpt in root.iter('trkpt'):
          row = trkpt.get('lat'), trkpt.get('lon')
          writer.writerow(row)
  </code></pre>
  
  <p>How do I do this? Please realize I'm a novice, so a more comprehensive explanation would be super awesome!</p>