#!/usr/bin/env python # version December 28, 2005 import sys,cPickle # You will need to have the data file "latest.mdf", which is available for download # at the tutorial. Or grab the fresh one at: # http://www.mesonet.org/data/public/mesonet/latest/latest.mdf # On Linux, you could run this script as: makePickle.py latest.mdf # Normally, the sys.argv list contains command line arguements, # which do not work in the IDLE of Windows. You could UNCOMMENT this next line to # simulate that command line arguments have been entered: # sys.argv=['bogus','latest.mdf',2,None] #set defaults: fileopen=False filename=None sep=None skiplines=2 print "sys.argv[0] always contains the name of the script:",sys.argv[0] #try to get a filename from the command line: try: filename=sys.argv[1] except: print "filename was not entered from command line, so" filename=raw_input("enter filename=>") #allow specification of number of lines to skip in the data file: try: skiplines=int(sys.argv[2]) except: print "assuming",skiplines,"lines to be skipped" #allow specification of a separator in the CSV file: try: sep=sys.argv[3] except: print "The seperator in the CSV file will be assumed to be white space" #try to open the file: while not fileopen: try: inf=open(filename,'r') fileopen=True except: print "trouble opening",filename,",try another name ..." filename=raw_input("enter filename=>") #process the CSV file into a dictionary of dictionaries: all=inf.readlines()[skiplines:] columntitles=all.pop(0) ct=columntitles.split() ct.pop(0) h={} for line in all: if len(line)<1: print "end of file" break v=line.split() key=v.pop(0) h[key]={} assert len(v)==len(ct) for i in range(len(ct)): h[key][ct[i]]=float(v[i]) #Done making the dictionary of dictionaries, now inspect it: mainkeys=h.keys() print "\n here are the keys to the main dictionary:",mainkeys samplekey=mainkeys[0] subkeys=h[samplekey].keys() print "\n using key",samplekey,", we find a dictionary with keys:", subkeys subkey=subkeys[0] print "\n using subkey",subkey,", here is a sample value:",h[samplekey][subkey] #now write a pickle file of the dictionary: oufn=filename+'.pickle' print "\n will attempt to pickle this dictionary of dictionaries in",oufn try: outfile=open(oufn,'w') cPickle.dump(h,outfile) except: print "what a shame, no pickle file could be written" print "All done!"