#!/usr/bin/python import cPickle verbose=False # set to True to activate more print statements oup=open('cgdata_040529.pickle','w') inf=open('040529.txt','r') lines=inf.readlines() #header=lines[1][1:] #fields=header.split('\t') #if verbose: print "\n".join(fields) hash={} #an empty dictionary. It will eventually hold two items. cgs=[] hash['Readme']="""cg data file; each item is an array with 7 items: Date Time(UTC) Latitude Longitude SignalStrength amplitude(kA) #ofstrikes""" for line in lines: if line[0]=="#": continue #skip comments in file line=line.strip() #strip leading and trailing white spaces v=line.split('\t') #split the string at tabs v=[x.strip() for x in v] #strip every item in v if verbose: print len(v),v cgs.append(v) hash['cgs']=cgs cPickle.dump(hash,oup) print "\n a dictionary with keys ",hash.keys()," was pickled in cgdata.pickle" print """\n you can load this dictionary with statement: anyname=cPickle.load('cgdata_040529.pickle')"""