#!/usr/bin/env python # # Convert csv to xml (eax4) for use with Flex/Air # # Alex Boschmans # # Function : explanation def usage(): print """ csv2xml.py - Convert a csv file with header line to an xml-type file (eax4) by Alex Boschmans (www.boschmans.net) Version 1.5 January 2009 --------------------------------------------------------------------------- Usage: Run csv2xml.py from the command line, supplying it with a csv filename. The csv file must have ; as a separator (this is the default if you export from MS Excel 2003). Using the first line as the header line for the xml elements, it will print out the e4x xml-type structure to the command line, so that you can then capture it by redirecting it to another file (either via the pipe command '|' in Linux or Unix to another cmmand or via the redirect command '>' in Windows. The xml format is of type e4x and can be read by for example ActionScript/Flex. Please be aware that : 1. Your header line should not contain any slashes ! (malformed xml element) 2. It will read each csv row only for the number of header elements. Anything after that will not be converted. Rows less long than the header will be silently dropped. - """ # Function : open the supplied file and read it in def readfile_supplied(filename_one): # Set the following variable to global so it gets used in the write_xml function global header # Todo : check that file exists at all ! # is the file empty ? if os.path.getsize(filename_one) == 0: exit("Error : filename given is empty") # Open the file via the csv parser reader = csv.reader(open (filename_one,'r'), delimiter=';') # Get the header header = reader.next() # Get the nr of elements in the header length = len(header) # Sanitize the header elements i = 0 while i < len(header): # First split them on spaces and then join them using underscores header[i] = string.join(string.split(header[i]),"_") # Replace any ( and ) header[i] = string.join(string.split(header[i],"("),"") header[i] = string.join(string.split(header[i],")"),"") i = i+1 # Write xml header and root print '' print "" # Then for each line, call the write_xml function to write the content out xml/eax4 fashion while True: try: # Pulling a row from the csv file (row is in list format) line = reader.next() # Make sure that the line is the exact length of the header, else drop it. if len(line) == length: write_xml(line, length) except StopIteration: # EOF, stop running the loop. break # Close the file print "" # Function : write xml def write_xml(content, length): # Write the xml in loop. # Each element in the content list becomes an xml element print " " i=0 while i < length: print " <" + header[i] + ">" + content[i] + "" i=i+1 print " " # Main Function if __name__ == '__main__': import os, sys, csv, string global filename error = 0 if len(sys.argv)== 2: # Processing arguments if (sys.argv[1] == '--help' or sys.argv[1] == '-h' or sys.argv[1] == '?' or sys.argv[1] == '/?'): error = 1 else: filename = sys.argv[1] else: error = 1 # Do it if error == 0: #print "Converting csv to xml structure, please wait..." readfile_supplied(filename) else: usage()