Re: File to dict



On Dec 7, 1:31 pm, mrk...@xxxxxxxxx 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()
.



Relevant Pages

  • Re: File to dict
    ... I have written this small utility function for transforming legacy ... file to Python dict: ... to transform files into dicts this way and I have quite a lot of such ... def _retrieve_next: ...
    (comp.lang.python)
  • File to dict
    ... file to Python dict: ... to transform files into dicts this way and I have quite a lot of such ... I would like to transform a file containing entries like ...
    (comp.lang.python)
  • Javascript - Pause/Play Animation Small Problem
    ... DEF Torso Transform{ ... #Body Route Node ...
    (comp.lang.vrml)
  • Re: Summing a 2D list
    ... So, writing C in python, which has dictionnary as builtin type, should be ... def c_dict(users, votes): ... print "with dict", do ...
    (comp.lang.python)
  • RE: unintuitive dict timings
    ... > # Test of various methods to add an item to a list in a dict. ... > def ifelse: ... You're timing lots of things besides what you're trying to time here. ... sequence of Python statements executed in ifelse and get1, ...
    (comp.lang.python)