Cleaner idiom for text processing?

has has.temp2 at virgin.net
Thu May 27 04:51:53 EDT 2004


mellis at frogwing.com (Michael Ellis) wrote in message news:<f2403000.0405251739.2a77b11d at posting.google.com>...
> 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?

If it's terseness you want:

    import re

    for line in infile:
        d = dict(re.findall('([^ ]+) ([^ ]+)', line))
        do_something_with_values(d['foo'], d['bar'])

Hardly worth worrying about though...



More information about the Python-list mailing list