How to insert IDs into Nodes when converting .csv to XML?
<p>Hello I'm new to Python,</p>

<p>and I would like to convert a <code>.csv</code>file to <code>XML</code>. The desired output should look like, where I would like to have each individual ID within a Node: <code>&lt;employee id="5"&gt;</code> and the variables corresponding to each individual beneath each other rather then on the same line:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;Document&gt;
  &lt;employee id="1"&gt;
    &lt;Name&gt;Steve&lt;/Name&gt;
    &lt;City&gt;Boston&lt;/City&gt;
    &lt;Age&gt;33&lt;/Age&gt;
  &lt;/employee&gt;
  &lt;employee id="2"&gt;
    &lt;Name&gt;Michael&lt;/Name&gt;
    &lt;City&gt;Dallas&lt;/City&gt;
    &lt;Age&gt;45&lt;/Age&gt;
  &lt;/employee&gt;
  &lt;employee id="3"&gt;
    &lt;Name&gt;John&lt;/Name&gt;
    &lt;City&gt;New York&lt;/City&gt;
    &lt;Age&gt;89&lt;/Age&gt;
  &lt;/employee&gt;
  &lt;employee id="4"&gt;
    &lt;Name&gt;Thomas&lt;/Name&gt;
    &lt;City&gt;LA&lt;/City&gt;
    &lt;Age&gt;62&lt;/Age&gt;
  &lt;/employee&gt;
  &lt;employee id="5"&gt;
    &lt;Name&gt;Clint&lt;/Name&gt;
    &lt;City&gt;Paris&lt;/City&gt;
    &lt;Age&gt;30&lt;/Age&gt;
  &lt;/employee&gt;
&lt;/Document&gt;
</code></pre>

<p>Given some data:</p>

<pre><code>import pandas
ID = pandas.DataFrame([1,2,3,4,5])
name = pandas.DataFrame(["Steve","Michael","John","Thomas","Clint"])
city = pandas.DataFrame(["Boston","Dallas","New York","LA","Paris"])
Age = pandas.DataFrame([45,33,33,20,50])
df = pandas.concat([ID, name,city,Age], axis=1)
df.columns = ['ID','name','city','Age']

df

    ID  name    city    Age

0   1   Steve   Boston  45
1   2   Michael Dallas  33
2   3   John    New York    33
3   4   Thomas  LA  20
4   5   Clint   Paris   50
</code></pre>

<p>And the conversion from <code>.csv</code> to <code>XML</code>:</p>

<pre><code>import csv

csvFile = 'df.csv'
xmlFile = 'myData.xml'

csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')
xmlData.write('&lt;?xml version="1.0"?&gt;' + "\n")
# there must be only one top-level tag
xmlData.write('&lt;Document&gt;' + "\n")

rowNum = 0
for employee in csvData:
    if rowNum == 0:
       tags = employee 
       # replace spaces w/ underscores in tag names
       for i in range(len(tags)):
           tags[i] = tags[i].replace(' ', '_')
    else: 
       xmlData.write('&lt;employee &gt;' + "\n")
       for i in range(len(tags)):
           xmlData.write('    ' + '&lt;' + tags[i] + '&gt;' \
                        + employee [i] + '&lt;/' + tags[i] + '&gt;' + "\n")
       xmlData.write('&lt;/employee &gt;' + "\n")

    rowNum +=1

xmlData.write('&lt;/Document&gt;' + "\n")
xmlData.close()
</code></pre>

<p>Output <code>XML</code> which looks a bit off as desired:</p>

<pre><code>&lt;&lt;?xml version="1.0"?&gt;
&lt;Document&gt;
&lt;employee&gt;
    &lt;X&gt;1&lt;/X&gt;
    &lt;ID&gt;1&lt;/ID&gt;
    &lt;Name&gt;Steve&lt;/Name&gt;
    &lt;City&gt;Boston&lt;/City&gt;
    &lt;Age&gt;33&lt;/Age&gt;
&lt;/employee&gt;
&lt;employee&gt;
    &lt;X&gt;2&lt;/X&gt;
    &lt;ID&gt;2&lt;/ID&gt;
    &lt;Name&gt;Michael&lt;/Name&gt;
    &lt;City&gt;Dallas&lt;/City&gt;
    &lt;Age&gt;45&lt;/Age&gt;
&lt;/employee&gt;
&lt;employee&gt;
    &lt;X&gt;3&lt;/X&gt;
    &lt;ID&gt;3&lt;/ID&gt;
    &lt;Name&gt;John&lt;/Name&gt;
   &lt;City&gt;New York&lt;/City&gt;
    &lt;Age&gt;89&lt;/Age&gt;
&lt;/employee&gt;
&lt;employee&gt;
    &lt;X&gt;4&lt;/X&gt;
    &lt;ID&gt;4&lt;/ID&gt;
    &lt;Name&gt;Thomas&lt;/Name&gt;
    &lt;City&gt;LA&lt;/City&gt;
    &lt;Age&gt;62&lt;/Age&gt;
&lt;/employee&gt;
 &lt;employee&gt;
    &lt;X&gt;5&lt;/X&gt;
    &lt;ID&gt;5&lt;/ID&gt;
    &lt;Name&gt;Clint&lt;/Name&gt;
    &lt;City&gt;Paris&lt;/City&gt;
    &lt;Age&gt;30&lt;/Age&gt;
&lt;/employee&gt;
&lt;/Document&gt;
</code></pre>

<p>You need to specify the delimiter of the csv file when creating the csv reader object (default is ',').</p>

<pre><code>csvData = csv.reader(open(csvFile), delimiter=' ')
</code></pre>

<p>If this is not given, then the entries of tags are not in the format you want.</p>

<hr>

<p>The else section in your for loop is not correct.
This should be the solution:</p>

<pre><code>import csv

csvFile = 'df.csv'
xmlFile = 'myData.xml'

csvData = csv.reader(open(csvFile), delimiter=';')
xmlData = open(xmlFile, 'w')
xmlData.write('&lt;?xml version="1.0"?&gt;' + "\n")
# there must be only one top-level tag
xmlData.write('&lt;Document&gt;' + "\n")

rowNum = 0
for employee in csvData:
    if rowNum == 0:
       tags = employee 
       # replace spaces w/ underscores in tag names
       for i in range(len(tags)):
           tags[i] = tags[i].replace(' ', '_')
    else: 
       xmlData.write('&lt;employee ' + tags[0] + '="' + employee[0] + '" &gt;' + "\n")
       for i in range(1,len(tags)):
           xmlData.write('    ' + '&lt;' + tags[i] + '&gt;' \
                        + employee [i] + '&lt;/' + tags[i] + '&gt;' + "\n")
       xmlData.write('&lt;/employee &gt;' + "\n")

    rowNum +=1

xmlData.write('&lt;/Document&gt;' + "\n")
xmlData.close()
</code></pre>

python