File to dict

mrkafk at gmail.com mrkafk at gmail.com
Fri Dec 7 07:44:25 EST 2007


Duncan Booth wrote:
> Just some minor points without changing the basis of what you have done
> here:

All good points, thanks. Phew, there's nothing like peer review for
your code...

> But why do you construct a dict from that input data simply to throw it
> away?

Because comparing strings for equality in a loop is writing C in
Python, and that's
exactly what I'm trying to unlearn.

The proper way to do it is to produce a dictionary and look up a value
using a key.

>If you only want 1 domain from the file just pick it out of the list.

for item in list:
    if item == 'searched.domain':
        return item...

Yuck.


>     with open('/etc/virtual/domainowners','r') as infile:
>         pairs = [ line.split(':',1) for line in infile if ':' in line ]

Didn't think about doing it this way. Good point. Thx




More information about the Python-list mailing list