Re: File to dict
- From: "J. Clifford Dyer" <jcd@xxxxxxxxxxxxxxxx>
- Date: Fri, 07 Dec 2007 22:12:08 -0500
On Fri, 2007-12-07 at 03:31 -0800, mrkafk@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).
Don't do more lookup than you have to to get the answer you need.
(untested code)
class DictFromFile(object):
def __init__(self, filename):
self.f = open(filename)
self.d = dict()
def __getitem__(self, key):
while key not in self.d:
try:
k, v = self._retrieve_next()
self.d[k] = v
if k == key:
break
except StopIteration:
break
return self.d[key]
def _retrieve_next(self):
line = self.f.next()
k, v = line.split(':',1)
return k.strip(), v.strip()
This will act like a dictionary, but only look as far into the file as
it needs to. If you've got a 10,000 line file, and all your results
come out of the first 10 lines, this will save you some processing.
There's more operator overloading you could do, of course.
Cheers,
Cliff
.
- References:
- File to dict
- From: mrkafk
- File to dict
- Prev by Date: sizers in wxpanels/notebooks
- Next by Date: RE: I'm missing something here with range vs. xrange
- Previous by thread: Re: File to dict
- Next by thread: Some python syntax that I'm not getting
- Index(es):
Relevant Pages
|