Created
May 9, 2013 14:43
-
-
Save AnthonyNystrom/5547886 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| csvFile = 'SMALLTEST.csv' | |
| xmlFile = 'myDataSmall.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