All pastes #56611 Raw Edit

jaeger

public python v1 · immutable
#56611 ·published 2006-05-17 17:27 UTC
rendered paste body
#!/usr/bin/env pythonimport os, re, sys# check command-line argumentsif len(sys.argv) < 4:	print "usage: " + sys.argv[0] + " <template file> <data file> <output file>";	sys.exit()# xml output header (created here to keep the template creation simple)xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"# xml output footerxml_footer = "\n"# read in the template file (1st command-line argument)try:	tmplif = open(sys.argv[1], 'r')	try:		template = tmplif.readlines()	finally:		tmplif.close()except IOError:	print "Failed to open template file " + sys.argv[1] + "!"	sys.exit()# read in the data file, using the first line as a header to define template# replacement values. If the header line contains# "Term","Section_Name","Person_ID","Campus_User_Name" then any occurences of# #Term, #Section_Name, #Person_ID, or #Campus_User_Name will be replaced with# the appropriate value from the csv data file in the resulting xml output.try:	dataif = open(sys.argv[2], 'r')	try:		# read all lines in the file into a list		datafile = dataif.readlines()		# use the first line to get column names (and remove trailing newline)		colnames = re.sub('\n$', '', datafile[0])		# remove quotation marks		colnames = re.sub('"', '', colnames)		# make a list of the column names		columns = colnames.split(',')		# remove the header from the data list now that column names are stored		datafile.reverse()		datafile.pop()		datafile.reverse()	finally:		dataif.close()except IOError:	print "Failed to open data file " + sys.argv[2] + "!"	sys.exit# all the necessary data is gathered now, try to open the output file (if it# exists it will be overwritten!)try:	xmlof = open(sys.argv[3], 'w')	try:		# write the xml header		xmlof.write(xml_header)		# for each entry in the data list, make an appropriate xml entry		# with template placeholders replaced by values from the data list		for line in datafile:			#print "Line: %d" % datafile.index(line)			# remove trailing newline			line = re.sub('\n$', '', line)			# remove quotation marks			line = re.sub('"', '', line)			print "\nCurrent entry: [%s]" % line			#continue			# split the line into a list of columns			linelist = line.split(',')			#print "Items on line: %d" % len(linelist)			#print "[%s] [%s] [%s] [%s]\n" % (linelist[0], linelist[1], linelist[2], linelist[3]),			skipline = 0			for line in linelist:				if len(line) == 0:					skipline = 1			if skipline == 1:				skipline = 0				break			# create a new xml entry from the template			for entry in template:				print "%s" % entry,			newxmlentry = template			for entry in newxmlentry:				print "%s" % entry,			print ""			# for each item in each datafile line			for item in linelist:				print "Current item: %s" % item				# now try to replace it in every line of the template				for tmplline in newxmlentry:					tmpindex = newxmlentry.index(tmplline)					#print "Index (newxmlentry line): %d" % tmpindex					#print "Template line: %s" % tmplline,					srchstr = "#" + columns[linelist.index(item)]					#print "Search string: %s" % srchstr					newline = tmplline.replace(srchstr, item)					newxmlentry[tmpindex] = newline					#print "New entry: %s" % newxmlentry[tmpindex],			for line in newxmlentry:				print line,							for newline in newxmlentry:				xmlof.write(newline)		# write the xml footer		xmlof.write(xml_footer)	finally:		xmlof.close()except IOError:	print "Failed to open output file " + sys.argv[3] + "!"	sys.exit()