On Thu, 28 May 2009 16:03:45 -0700 (PDT) Marius Retegan <[email protected]> wrote:
> Hello
> I have simple text file that I have to parse. It looks something like
> this:
>
> parameters1
> key1 value1
> key2 value2
> end
>
> parameters2
> key1 value1
> key2 value2
> end
>
> So I want to create two dictionaries parameters1={key1:value1,
> key2:value2} and the same for parameters2.
You can use iterators to efficiently parse no-matter-how-large file.
Following code depends on line breaks and 'end' statement rather than
indentation.
import itertools as it, operator as op, functools as ft
from string import whitespace as spaces
with open('test.src') as src:
lines = it.ifilter(bool, it.imap(lambda x: x.strip(spaces), src))
sections = ( (lines.next(), dict(it.imap(str.split, lines))) for sep,lines
in
it.groupby(lines, key=lambda x: x == 'end') if not sep )
data = dict(sections)
print data
# { 'parameters2': {'key2': 'value2', 'key1': 'value1'},
# 'parameters1': {'key2': 'value2', 'key1': 'value1'} }
To save namespace and make it a bit more unreadable you can write it
as a one-liner:
with open('test.src') as src:
data = dict( (lines.next(), dict(it.imap(str.split, lines))) for sep,lines
in
it.groupby(it.ifilter(bool, it.imap(lambda x: x.strip(spaces), src)),
key=lambda x: x == 'end') if not sep )
--
Mike Kazantsev // fraggod.net
signature.asc
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list
