I have the CSV with the following column headers:
"G","Association","Subset","Rank","First Name","Last Name","Middle Name","Date of Birth","SS","Drivers License Number","Address","Phone Number","A","CF","CM","I","F","J","CN","I","CN"
The Output XML format is what I seek:
<GIs>
<GIR>
<G></G>
<GM>
<FirstName></FirstName>
<LastName></LastName>
<MiddleName></MiddleName>
<FullName></FullName>
<DateofBirth></DateofBirth>
<SS></SS>
<DriversLicenseNumber></DriversLicenseNumber>
<Address>
<AddressText></AddressText>
<Latitude></Latitude>
<Longitude></Longitude>
</Address>
<PhoneNumber></PhoneNumber>
<A></A>
<CF></CF>
<CM></CM>
<Association></Association>
<Subset></Subset>
<Rank></Rank>
<I></I>
<F></F>
<J></J>
</GM>
<GI>
<CN></CN>
<I></I>
<CN></CN>
</CI>
</GIR>
</GIs>
I am wondering how to most efficiently do this? Here is what I have tried so far:
import csv
# Stuff from the XML module
from xml.etree.ElementTree import Element, SubElement, tostring
# Topmost XML element
top = Element('GIs')
# Open a file
with open('stuff.csv') as csvfile:
# And use a dictionary-reader
for d in csv.DictReader(csvfile)
# For each mapping in the dictionary
for (k, v) in d.iteritems():
# Create an XML node
child = SubElement(top, k)
child.text = v
print tostring(top)
Any guidance would be much appreciated.