P
ProvoWallis
Hi,
Would anyone be willing to give me some feedback about this little
script that I wrote to convert CSV to XML. I'll happily admit that I
still have a lot to learn about Python so I'm always grateful for
constructive feedback.
Thanks,
Greg
###
#csv to XML conversion utility
import os, re, csv
root = raw_input("Enter the path where the program should run: ")
fname = raw_input("Enter name of the uncoverted file: ")
print
given,ext = os.path.splitext(fname)
root_name = os.path.join(root,fname)
n = given + '.xml'
outputName = os.path.join(root,n)
reader = csv.reader(open(root_name, 'r'), delimiter=',')
output = open(outputName, 'w')
output.write('<?xml version="1.0"
encoding="utf-8"?><em:table>\n<core:title/>')
output.write('\n<core:legend> %s %s </core:legend>\n<table
frame="none" colsep="0" rowsep="0">\n<tgroup cols="11" colsep="0"
rowsep="0">\n<tbody valign="bottom">' % ('TAS input file for ', given))
for row in reader:
for i in range(0, len(row)):
if i == 0:
output.write('\n<row>\n<entry
colname="col%s">%s</entry>' % (i, row))
if i > 0 and i < len(row) - 1:
output.write('\n<entry colname="col%s">%s</entry>' % (i,
row))
if i == len(row) - 1:
output.write('\n<entry
colname="col%s">%s</entry>\n</row>' % (i, row))
output.write('\n</tbody>\n</tgroup>\n</table>\n</em:table>')
output.close()
Would anyone be willing to give me some feedback about this little
script that I wrote to convert CSV to XML. I'll happily admit that I
still have a lot to learn about Python so I'm always grateful for
constructive feedback.
Thanks,
Greg
###
#csv to XML conversion utility
import os, re, csv
root = raw_input("Enter the path where the program should run: ")
fname = raw_input("Enter name of the uncoverted file: ")
given,ext = os.path.splitext(fname)
root_name = os.path.join(root,fname)
n = given + '.xml'
outputName = os.path.join(root,n)
reader = csv.reader(open(root_name, 'r'), delimiter=',')
output = open(outputName, 'w')
output.write('<?xml version="1.0"
encoding="utf-8"?><em:table>\n<core:title/>')
output.write('\n<core:legend> %s %s </core:legend>\n<table
frame="none" colsep="0" rowsep="0">\n<tgroup cols="11" colsep="0"
rowsep="0">\n<tbody valign="bottom">' % ('TAS input file for ', given))
for row in reader:
for i in range(0, len(row)):
if i == 0:
output.write('\n<row>\n<entry
colname="col%s">%s</entry>' % (i, row))
if i > 0 and i < len(row) - 1:
output.write('\n<entry colname="col%s">%s</entry>' % (i,
row))
if i == len(row) - 1:
output.write('\n<entry
colname="col%s">%s</entry>\n</row>' % (i, row))
output.write('\n</tbody>\n</tgroup>\n</table>\n</em:table>')
output.close()