I’m learning how to parse CSV files and format the data into XML files using ElementTree and minidom (for pretty printing). I’ve gotten so close, but hit one little snag that I can’t seem to wrap my head around. I have the following code:
for csvFile in directory:
root = Element('Order')
with open(csvFile, 'rt') as f:
reader = csv.reader(f)
for row in reader:
queue = row[0]
token = row[1]
current_order = SubElement(root, 'Token', {'text':token})
details = SubElement(current_order, 'Queue',
{'queue':queue})
print prettify(root)
Which produces the following XML:
<?xml version="1.0" ?>
<Order>
<Token text="token">
<Queue queue="site_code"/>
</Token>
<Token text="H54FC9">
<Queue queue="Duplex_N-up"/>
</Token>
</Order>
How can I strip the headers from the CSV input so that the XML looks like this:
<?xml version="1.0" ?>
<Order>
<Token text="H54FC9">
<Queue queue="Duplex_N-up"/>
</Token>
</Order>
Thank you!
Are you looking to discard the first line from your csv file? If so, you can do that pretty easily: