Skip to content

Instantly share code, notes, and snippets.

@nanda-dash
Created January 27, 2017 17:50
Show Gist options
  • Save nanda-dash/5b5875924e740d4f5f5c3f17aa7300f5 to your computer and use it in GitHub Desktop.
Save nanda-dash/5b5875924e740d4f5f5c3f17aa7300f5 to your computer and use it in GitHub Desktop.
CSV to XML
# csv2xml.py
# First row of the csv file must be header!
# example CSV file: myData.csv
# id,code name,value
# 36,abc,7.6
# 40,def,3.6
# 9,ghi,6.3
# 76,def,99
import csv
csvFile = 'd:\\myData.csv'
xmlFile = 'd:\\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('<csv_data>' + "\n")
rowNum = 0
for row in csvData:
if rowNum == 0:
tags = row
# replace spaces w/ underscores in tag names
for i in range(len(tags)):
tags[i] = tags[i].replace(' ', '_')
else:
xmlData.write('<row>' + "\n")
for i in range(len(tags)):
xmlData.write(' ' + '<' + tags[i] + '>' \
+ row[i] + '</' + tags[i] + '>' + "\n")
xmlData.write('</row>' + "\n")
rowNum +=1
xmlData.write('</csv_data>' + "\n")
xmlData.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment