title: Parsing XML and writing to CSV file
body: <p>I am parsing a simple XML document with a simple script that I wrote (with a couple of tweaks). Here is the XML:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; ?&gt;
  &lt;library owner=&quot;John Franks&quot;&gt;
   &lt;book&gt;
    &lt;title&gt;Sandman Volume 1: Preludes and Nocturnes&lt;/title&gt;
    &lt;author&gt;Neil Gaiman&lt;/author&gt;
   &lt;/book&gt;
   &lt;book&gt;
    &lt;title&gt;Good Omens&lt;/title&gt;
    &lt;author&gt;Neil Gamain&lt;/author&gt;
    &lt;author&gt;Terry Pratchett&lt;/author&gt;
   &lt;/book&gt;
   &lt;book&gt;
    &lt;title&gt;The Man And The Goat&lt;/title&gt;
    &lt;author&gt;Bubber Elderidge&lt;/author&gt;
   &lt;/book&gt;
   &lt;book&gt;
    &lt;title&gt;Once Upon A Time in LA&lt;/title&gt;
    &lt;author&gt;Dr Dre&lt;/author&gt;
   &lt;/book&gt;
   &lt;book&gt;
    &lt;title&gt;There Will Never Be Justice&lt;/title&gt;
    &lt;author&gt;IR Jury&lt;/author&gt;
   &lt;/book&gt;
   &lt;book&gt;
    &lt;title&gt;Beginning Python&lt;/title&gt;
    &lt;author&gt;Peter Norton, et al&lt;/author&gt;
   &lt;/book&gt;
  &lt;/library&gt;
  </code></pre>
  <p>And here is my Python script:</p>
  <pre><code>from xml.dom.minidom import parse
  import xml.dom.minidom
  import csv
  
  def writeToCSV(myLibrary):
    csvfile = open('output.csv', 'w')
    fieldnames = ['title', 'author', 'author']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    
    books = myLibrary.getElementsByTagName(&quot;book&quot;)
    for book in books:
      titleValue = book.getElementsByTagName(&quot;title&quot;)[0].childNodes[0].data
      for author in book.getElementsByTagName(&quot;author&quot;):
        authorValue = author.childNodes[0].data
        writer.writerow({'title': titleValue, 'author': authorValue})
  
  
  doc = parse('library.xml')
  myLibrary = doc.getElementsByTagName(&quot;library&quot;)[0]
  
  # Get book elements in Library
  books = myLibrary.getElementsByTagName(&quot;book&quot;)
  
  # Print each book's title
  writeToCSV(myLibrary)
  </code></pre>
  <p>Here is my output:</p>
  <pre><code>title,author
  
  Sandman Volume 1: Preludes and Nocturnes,Neil Gaiman
  
  Good Omens,Neil Gamain
  
  Good Omens,Terry Pratchett
  
  The Man And The Goat,Bubber Elderidge
  
  Once Upon A Time in LA,Dr Dre
  
  There Will Never Be Justice,IR Jury
  
  Beginning Python,&quot;Peter Norton, et al&quot;
  </code></pre>
  <p>Notice that the book <em>&quot;Good Omens&quot;</em> has 2 authors, and is displayed on two separate lines. What I would really like is for it to display as follows:</p>
  <pre><code>title,author,author
  
  Sandman Volume 1: Preludes and Nocturnes,Neil Gaiman,,
  
  Good Omens,Neil Gamain,Terry Pratchett
  
  The Man And The Goat,Bubber Elderidge,,
  
  Once Upon A Time in LA,Dr Dre,,
  
  There Will Never Be Justice,IR Jury,,
  
  Beginning Python,&quot;Peter Norton, et al&quot;,,
  </code></pre>
  <p>As you can see there are 3 columns, so the two authors display on the same line. Those books that have only one author, there is simply a blank entry, so two commas next to each other.</p>