Cleaner idiom for text processing?

Michael Ellis mellis at frogwing.com
Tue May 25 21:39:04 EDT 2004


Hi,
I have some data files with lines in space-delimited <name> <value>
format. There are multiple name-value pairs per line. 

Is there a cleaner idiom than the following for reading each line into
an associative array for the purpose of accessing values by name?

for line in infile:
   tokens = line.split()
   dict = {}
   for i in range(0, len(tokens),2) dict[tokens[i]] = tokens[i+1]
   do_something_with_values(dict['foo'],dict['bar'])

Thanks!
Mike Ellis



More information about the Python-list mailing list