File to dict

mrkafk at gmail.com mrkafk at gmail.com
Fri Dec 7 12:51:39 EST 2007



Glauco wrote:

> cache = None
>
> def lookup( domain ):
>     if not cache:
>        cache = dict( [map( lambda x: x.strip(), x.split(':'))  for x in
> open('/etc/virtual/domainowners','r').readlines()])
>     return cache.get(domain)

Neat solution! It just needs small correction for empty or badly
formed lines:

dict([map( lambda x: x.strip(), x.split(':'))  for x in open('/etc/
virtual/domainowners','r') if ':' in x])




More information about the Python-list mailing list