File to dict

Chris cwitts at gmail.com
Fri Dec 7 06:51:22 EST 2007


On Dec 7, 1:31 pm, mrk... at gmail.com wrote:
> Hello everyone,
>
> I have written this small utility function for transforming legacy
> file to Python dict:
>
> def lookupdmo(domain):
>         lines = open('/etc/virtual/domainowners','r').readlines()
>         lines = [ [y.lstrip().rstrip() for y in x.split(':')] for x in
> lines]
>         lines = [ x for x in lines if len(x) == 2 ]
>         d = dict()
>         for line in lines:
>                 d[line[0]]=line[1]
>         return d[domain]
>
> The /etc/virtual/domainowners file contains double-colon separated
> entries:
> domain1.tld: owner1
> domain2.tld: own2
> domain3.another: somebody
> ...
>
> Now, the above lookupdmo function works. However, it's rather tedious
> to transform files into dicts this way and I have quite a lot of such
> files to transform (like custom 'passwd' files for virtual email
> accounts etc).
>
> Is there any more clever / more pythonic way of parsing files like
> this? Say, I would like to transform a file containing entries like
> the following into a list of lists with doublecolon treated as
> separators, i.e. this:
>
> tm:$1$aaaa$bbbb:1010:6::/home/owner1/imap/domain1.tld/tm:/sbin/nologin
>
> would get transformed into this:
>
> [ ['tm', '$1$aaaa$bbbb', '1010', '6', , '/home/owner1/imap/domain1.tld/
> tm', '/sbin/nologin'] [...] [...] ]

For the first one you are parsing the entire file everytime you want
to lookup just one domain.  If it is something reused several times
during your code execute you could think of rather storing it so it's
just a simple lookup away, for eg.

_domain_dict = dict()
def generate_dict(input_file):
    finput = open(input_file, 'rb')
    global _domain_dict
    for each_line in enumerate(finput):
        line = each_line.strip().split(':')
        if len(line)==2: _domain_dict[line[0]] = line[1]

    finput.close()

def domain_lookup(domain_name):
    global _domain_dict
    try:
        return _domain_dict[domain_name]
    except KeyError:
        return 'Unknown.Domain'


Your second parsing example would be a simple case of:

finput = open('input_file.ext', 'rb')
results_list = []
for each_line in enumerate(finput.readlines()):
    results_list.append( each_line.strip().split(':') )
finput.close()



More information about the Python-list mailing list