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>(posted as an answer so I can show a code block)</p>

<p>There are a lot of picky details when writing XML.  In Python, you should probably use some version of ElementTree to help with that.  One good tutorial is <a href="https://pymotw.com/2/xml/etree/ElementTree/create.html" rel="nofollow" title="PyMOTW">Creating XML Documents</a>.  Quoting from there:</p>

<pre><code>from xml.etree.ElementTree import Element, SubElement, Comment, tostring

top = Element('top')

comment = Comment('Generated for PyMOTW')
top.append(comment)

child = SubElement(top, 'child')
child.text = 'This child contains text.'

child_with_tail = SubElement(top, 'child_with_tail')
child_with_tail.text = 'This child has regular text.'
child_with_tail.tail = 'And "tail" text.'

child_with_entity_ref = SubElement(top, 'child_with_entity_ref')
child_with_entity_ref.text = 'This &amp; that'

print(tostring(top))
</code></pre>

<p>If you use this as an example of how to create a tree of XML elements, you should be able to translate your code into the XML structure you need.</p>

python xml csv