Convert CSV document to XML
<p>I know the question is redundant but I tried all the Python code that I found and modified for my file but they did not work. I need to find a way to convert my file <a href="http://opendata.paris.fr/explore/dataset/tournagesdefilmsparis2011" rel="nofollow">myData.csv</a> in to a XML format file which can be read by a navigator. </p>

<p>I just started to learn Python this month so I'm a beginner. This is my code:</p>

<pre><code>#! usr/bin/python
# -*- coding: utf-8 -*-
import csv, sys, os
from lxml import etree


csvFile = 'myData.csv' # crÃ©ation de la variable pour le fichier csv
reader= csv.reader(open(csvFile), delimiter=';', quoting=csv.QUOTE_NONE) # crÃ©ation d'une variable reader Ã  qui on renvoie le tableau csv

print "&lt;data&gt;"
for record in reader:
    if reader.line_num == 1:
        header = record
    else:
        innerXml = ""
        dontShow = False
        type = ""
        for i, field in enumerate(record):
            innerXml += "&lt;%s&gt;" % header[i].lower() + field + "&lt;/%s&gt;" % header[i].lower()
            if i == 1 and field == "0":
                type = "Next"
            elif type == "" and i == 3 and field == "0":
                type = "Next"
            elif type == "" and i == 3 and field != "0":
                type = "film"

            if i == 1 and field == "X":
                dontShow = True

        if dontShow == False:
            xml = "&lt;%s&gt;" % type
            xml += innerXml
            xml += "&lt;/%s&gt;" % type
            print xml
print "&lt;/data&gt;"
</code></pre>

<p>Consider building your XML with dedicated DOM objects and not a concatenation of strings which you can do with the <code>lxml</code> module. Using methods such as <code>Element()</code>, <code>SubElement()</code>, etc. you can iteratively build XML tree from reading CSV data:</p>

<pre><code>import csv
import lxml.etree as ET

headers = ['Titre', 'Realisateur', 'Date_Debut_Evenement', 'Date_Fin_Evenement', 'Cadre',
           'Lieu', 'Adresse', 'Arrondissement', 'Adresse_complÃ¨te', 'Geo_Coordinates']

# INITIALIZING XML FILE
root = ET.Element('root')

# READING CSV FILE AND BUILD TREE
with open('myData.csv') as f:
    next(f)                             # SKIP HEADER
    csvreader = csv.reader(f)

    for row in csvreader:        
        data = ET.SubElement(root, "data")
        for col in range(len(headers)):
            node = ET.SubElement(data, headers[col]).text = str(row[col])

# SAVE XML TO FILE
tree_out = (ET.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8"))

# OUTPUTTING XML CONTENT TO FILE
with open('Output.xml', 'wb') as f:
    f.write(tree_out)
</code></pre>

<p><strong>Output</strong></p>

<pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;root&gt;
  &lt;data&gt;
    &lt;Titre&gt;1&lt;/Titre&gt;
    &lt;Realisateur&gt;BUS PALLADIUM&lt;/Realisateur&gt;
    &lt;Date_Debut_Evenement&gt;CHRISTOPHER THOMPSON&lt;/Date_Debut_Evenement&gt;
    &lt;Date_Fin_Evenement&gt;21 mai 2009&lt;/Date_Fin_Evenement&gt;
    &lt;Cadre&gt;21 mai 2009&lt;/Cadre&gt;
    &lt;Lieu&gt;EXTERIEUR&lt;/Lieu&gt;
    &lt;Adresse&gt;PLACE&lt;/Adresse&gt;
    &lt;Arrondissement&gt;PIGALLE&lt;/Arrondissement&gt;
    &lt;Adresse_complÃ¨te&gt;75018&lt;/Adresse_complÃ¨te&gt;
    &lt;Geo_Coordinates&gt;PLACE PIGALLE 75018 Paris France&lt;/Geo_Coordinates&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;Titre&gt;2&lt;/Titre&gt;
    &lt;Realisateur&gt;LES INVITES DE MON PERE&lt;/Realisateur&gt;
    &lt;Date_Debut_Evenement&gt;ANNE LE NY&lt;/Date_Debut_Evenement&gt;
    &lt;Date_Fin_Evenement&gt;20 mai 2009&lt;/Date_Fin_Evenement&gt;
    &lt;Cadre&gt;20 mai 2009&lt;/Cadre&gt;
    &lt;Lieu&gt;DOMAINE PUBLIC&lt;/Lieu&gt;
    &lt;Adresse&gt;SQUARE&lt;/Adresse&gt;
    &lt;Arrondissement&gt;DU CLIGNANCOURT&lt;/Arrondissement&gt;
    &lt;Adresse_complÃ¨te&gt;75018&lt;/Adresse_complÃ¨te&gt;
    &lt;Geo_Coordinates&gt;SQUARE DU CLIGNANCOURT 75018 Paris France&lt;/Geo_Coordinates&gt;
  &lt;/data&gt;
  &lt;data&gt;
    &lt;Titre&gt;3&lt;/Titre&gt;
    &lt;Realisateur&gt;DEMAIN, A L'AUBE&lt;/Realisateur&gt;
    &lt;Date_Debut_Evenement&gt;GAEL CABOUAT&lt;/Date_Debut_Evenement&gt;
    &lt;Date_Fin_Evenement&gt;17 avril 2009&lt;/Date_Fin_Evenement&gt;
    &lt;Cadre&gt;17 avril 2009&lt;/Cadre&gt;
    &lt;Lieu&gt;EXTERIEUR&lt;/Lieu&gt;
    &lt;Adresse&gt;RUE&lt;/Adresse&gt;
    &lt;Arrondissement&gt;QUINCAMPOIX&lt;/Arrondissement&gt;
    &lt;Adresse_complÃ¨te&gt;75004&lt;/Adresse_complÃ¨te&gt;
    &lt;Geo_Coordinates&gt;RUE QUINCAMPOIX 75004 Paris France&lt;/Geo_Coordinates&gt;
  &lt;/data&gt;
  ...
</code></pre>

python xml csv