Hello I'm new to Python,
and I would like to convert a .csv
file to XML
. The desired output should look like, where I would like to have each individual ID within a Node: <employee id="5">
and the variables corresponding to each individual beneath each other rather then on the same line:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<employee id="1">
<Name>Steve</Name>
<City>Boston</City>
<Age>33</Age>
</employee>
<employee id="2">
<Name>Michael</Name>
<City>Dallas</City>
<Age>45</Age>
</employee>
<employee id="3">
<Name>John</Name>
<City>New York</City>
<Age>89</Age>
</employee>
<employee id="4">
<Name>Thomas</Name>
<City>LA</City>
<Age>62</Age>
</employee>
<employee id="5">
<Name>Clint</Name>
<City>Paris</City>
<Age>30</Age>
</employee>
</Document>
Given some data:
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
And the conversion from .csv
to XML
:
import csv
csvFile = 'df.csv'
xmlFile = 'myData.xml'
csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')
xmlData.write('<?xml version="1.0"?>' + "\n")
# there must be only one top-level tag
xmlData.write('<Document>' + "\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('<employee >' + "\n")
for i in range(len(tags)):
xmlData.write(' ' + '<' + tags[i] + '>' \
+ employee [i] + '</' + tags[i] + '>' + "\n")
xmlData.write('</employee >' + "\n")
rowNum +=1
xmlData.write('</Document>' + "\n")
xmlData.close()
Output XML
which looks a bit off as desired:
<<?xml version="1.0"?>
<Document>
<employee>
<X>1</X>
<ID>1</ID>
<Name>Steve</Name>
<City>Boston</City>
<Age>33</Age>
</employee>
<employee>
<X>2</X>
<ID>2</ID>
<Name>Michael</Name>
<City>Dallas</City>
<Age>45</Age>
</employee>
<employee>
<X>3</X>
<ID>3</ID>
<Name>John</Name>
<City>New York</City>
<Age>89</Age>
</employee>
<employee>
<X>4</X>
<ID>4</ID>
<Name>Thomas</Name>
<City>LA</City>
<Age>62</Age>
</employee>
<employee>
<X>5</X>
<ID>5</ID>
<Name>Clint</Name>
<City>Paris</City>
<Age>30</Age>
</employee>
</Document>