a couple of newbie questions

John Machin sjmachin at lexicon.net
Sat Mar 22 03:38:29 CET 2003


Nick Vargish <nav at adams.patriot.net> wrote in message news:<yyyvfyck23q.fsf at adams.patriot.net>...
> "Joseph Paish" <jpaish at freenet.edmonton.ab.ca> writes:
> > 2.  is there a way to read a datafile whose records are space
> > separated so that the second field becomes the key to a dictionary
> > and the value portion is the entire record.  for example,
>  
> > 01/01/2003 abc 1234.56 789.98 12332.98 <<< the original record
> 
> datadict = {}
> datafile = open(myfile, 'r')
> for dataline in datafile.xreadlines():
>     datadict[dataline.split(' ')[1]] = dataline
> 
> That should do the trick... 
> 

Unless you give a damn about data integrity, in which case you might
like to check things like (1) do lines have the minimum 2 fields and
the presumably desirable 5 fields [based on the OP's example] (2) are
there any duplicate keys.




More information about the Python-list mailing list