M
Michael Ellis
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] = tokens[i+1]
do_something_with_values(dict['foo'],dict['bar'])
Thanks!
Mike Ellis
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] = tokens[i+1]
do_something_with_values(dict['foo'],dict['bar'])
Thanks!
Mike Ellis