Python

1. CSV to XML, Python solution

1.

CSV to XML, Python solution

DaveP

One of the jobs I've picked up recently involved csv to xml.

the following script does this job nicely. tags holds the required tags. csv package from object-craft.com.au

In the hope that it might be useful.


import sys
import string
import csv


tags
=["BIBNO","CATALOGUE","CATEGORY","COMMENTS","NO_CASS","PLAYING_TIME",
"READER_GENDER","SEQUEL","WARNING","M100","M655","READERS","513","M700_1",
"M245","M260","M008","M440","MEXTRA1T","MEXTRA1","MEXTRA2T","MEXTRA2",
"MEXTRA3T","MEXTRA3"]
p = csv.parser()
p.field_sep = '|'
f = open("dump230102.txt")
while 1:
    l = f.readline()
    if not l:
        break
    r = p.parse(l)
 
    if r is not None:
        print "<tag>"
        for i in r:
            print "<"+tags[r.index(i)]+">"+i+"</"+tags[r.index(i)]+">"
        print "</tag>"