# csv2xml.py # FB - 201010107 # 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 = 'myData.csv' xmlFile = 'myData.xml' csvData = csv.reader(open(csvFile)) xmlData = open(xmlFile, 'w') xmlData.write('' + "\n") # there must be only one top-level tag xmlData.write('' + "\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('' + "\n") for i in range(len(tags)): xmlData.write(' ' + '<' + tags[i] + '>' \ + row[i] + '' + "\n") xmlData.write('' + "\n") rowNum +=1 xmlData.write('' + "\n") xmlData.close()