############################################################# # csv2xml.py # A simple python script which converts the UNSPSC CSV # format file to an XML form suitable for XSLT processing # into a topic map. # # (c) 2002 Khalil Ahmed # Permission is granted to use, copy and redistribute this # file as long as this notice remains intact. # The author requests to be notified of any significant # enhancements or bug fixes made to this file. Notification # may be made by email to kal@techquila.com ############################################################## f = open('11UNSPSC.csv', 'r') lines = f.readlines() o = open('11UNSPSC.xml', 'w+') o.write('') o.write('') inseg = 0 infam = 0 incls = 0 def write_meta(o, toks): o.write('%s\n' % toks[4]) o.write('\n' % (toks[5], toks[6][:-1])) o.write('%s.%s.%s.%s\n' % (toks[0], toks[1], toks[2], toks[3])) for l in lines[1:]: toks = l.split(",") if (len(toks) != 7): print("Error - found a line with more than 7 columns.") if (toks[1] == "00"): if (incls): o.write('\n') incls = 0 if (infam): o.write('\n') infam = 0 if (inseg): o.write('\n') inseg = 1 o.write('\n' % toks[0]) write_meta(o, toks) elif(toks[2] == "00"): if (incls): o.write('\n') incls = 0 if (infam): o.write('\n') infam = 1 o.write('\n' % toks[1]) write_meta(o, toks) elif(toks[3] == "00"): if (incls): o.write('\n') incls = 1 o.write('\n' % toks[2]) write_meta(o, toks) else: o.write('' % toks[3]) write_meta(o, toks) o.write('\n') o.write('\n') o.write('\n') o.write('\n') o.write('') o.close() f.close()