Newbie Question: CSV to XML

P

ProvoWallis

Hi,

I'm learning more and more about Python all the time but I'm still a
real newbie. I wrote this little script to convert CSV to XML and I was
hoping to get some feedback on it if anyone was willing to comment.

It works but I was wondering if there was anything I could do better.
E.g., incorporate minidom somehow? But I'm totally in the dark as how I
would do this.

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"?>\n<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()
 
M

Max Erickson

Hi,

I'm learning more and more about Python all the time but I'm still a
real newbie. I wrote this little script to convert CSV to XML and I was
hoping to get some feedback on it if anyone was willing to comment.

It works but I was wondering if there was anything I could do better.
E.g., incorporate minidom somehow? But I'm totally in the dark as how I
would do this.

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: ")

consider parsing the command line to get the file to convert, ie
csvtoxml.py FILE

searching on sys.argv, getopt and optparse will give you lots of info on
this.

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))


instead of testing for the first and last rows, just write the row
stuff in the outer loop. Untested, but it should work the same...

for row in reader:
output.write('\n<row>')
for i, cell in enumerate(row):
output.write('\n<entry colname="col%s">%s</entry>' % (i,cell)
output.write('\n</row>')


max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top