File to dict

mrkafk at gmail.com mrkafk at gmail.com
Fri Dec 7 08:50:13 EST 2007


> I guess Duncan's point wasn't the construction of the dictionary but the
> throw it away part.  If you don't keep it, the loop above is even more
> efficient than building a dictionary with *all* lines of the file, just to
> pick one value afterwards.

Sure, but I have two options here, none of them nice: either "write C
in Python" or do it inefficient and still elaborate way.

Anyway, I found my nirvana at last:

>>> def shelper(line):
...     return x.replace(' ','').strip('\n').split(':',1)
...

>>> ownerslist = [ shelper(x)[1] for x in it if len(shelper(x)) == 2 and shelper(x)[0] == domain ]

>>> ownerslist
['da2']


Python rulez. :-)







More information about the Python-list mailing list