title: Simple CSV to XML Conversion - Python
body: <p>I am looking for a way to automate the conversion of CSV to XML. </p>
  
  <p>Here is an example of a CSV file, containing a list of movies:</p>
  
  <p><a href="https://i.stack.imgur.com/Htvuy.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Htvuy.jpg" alt="Movies Csv"></a></p>
  
  <p>Here is the file in XML format:</p>
  
  <pre><code>&lt;collection shelf="New Arrivals"&gt;
  &lt;movietitle="Enemy Behind"&gt;
     &lt;type&gt;War, Thriller&lt;/type&gt;
     &lt;format&gt;DVD&lt;/format&gt;
     &lt;year&gt;2003&lt;/year&gt;
     &lt;rating&gt;PG&lt;/rating&gt;
     &lt;stars&gt;10&lt;/stars&gt;
     &lt;description&gt;Talk about a US-Japan war&lt;/description&gt;
  &lt;/movie&gt;
  &lt;movietitle="Transformers"&gt;
     &lt;type&gt;Anime, Science Fiction&lt;/type&gt;
     &lt;format&gt;DVD&lt;/format&gt;
     &lt;year&gt;1989&lt;/year&gt;
     &lt;rating&gt;R&lt;/rating&gt;
     &lt;stars&gt;8&lt;/stars&gt;
     &lt;description&gt;A schientific fiction&lt;/description&gt;
  &lt;/movie&gt;
  &lt;movietitle="Trigun"&gt;
     &lt;type&gt;Anime, Action&lt;/type&gt;
     &lt;format&gt;DVD&lt;/format&gt;
     &lt;episodes&gt;4&lt;/episodes&gt;
     &lt;rating&gt;PG&lt;/rating&gt;
     &lt;stars&gt;10&lt;/stars&gt;
     &lt;description&gt;Vash the Stampede!&lt;/description&gt;
  &lt;/movie&gt;
  &lt;movietitle="Ishtar"&gt;
     &lt;type&gt;Comedy&lt;/type&gt;
     &lt;format&gt;VHS&lt;/format&gt;
     &lt;rating&gt;PG&lt;/rating&gt;
     &lt;stars&gt;2&lt;/stars&gt;
     &lt;description&gt;Viewable boredom&lt;/description&gt;
  &lt;/movie&gt;
  &lt;/collection&gt;
  </code></pre>
  
  <p>I've tried a few examples where I am able to read the csv and XML format using Python using DOM and SAX but yet am to find a simple example of the conversion. So far I have:</p>
  
  <pre><code>import csv              
  f = open('movies2.csv')
  csv_f = csv.reader(f)   
  
  def convert_row(row):
     return """&lt;movietitle="%s"&gt;
     &lt;type&gt;%s&lt;/type&gt;
     &lt;format&gt;%s&lt;/format&gt;
     &lt;year&gt;%s&lt;/year&gt;
     &lt;rating&gt;%s&lt;/rating&gt;
     &lt;stars&gt;%s&lt;/stars&gt;
     &lt;description&gt;%s&lt;/description&gt;
  &lt;/movie&gt;""" % (
     row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)
  
  print ('\n'.join(csv_f.apply(convert_row, axis=1)))
  </code></pre>
  
  <p>But I get the error: </p>
  
  <pre><code> File "moviesxml.py", line 16, in module
     print ('\n'.join(csv_f.apply(convert_row, axis=1)))
  AttributeError: '_csv.reader' object has no attribute 'apply'
  </code></pre>
  
  <p>I am pretty new to Python, so any help would be much appreciated!</p>
  
  <p>I am using Python 3.5.2.</p>
  
  <p>Thanks!</p>
  
  <p>Lisa</p>