How to create a dict based on such a file?

Dan Stromberg drsalists at gmail.com
Mon Feb 14 14:25:00 EST 2011


On Sun, Feb 13, 2011 at 9:47 PM, Wang Coeus <wangcoeus at gmail.com> wrote:
> Hi all,
> I am new to python. Currently I encountered a problem, please help me to
> solve this. Thanks in advance!
> I have a file like below:
> ++++++++++++++++++++++++++++++++++++++
> block1
> {
>  key1=value1
>  key2=value2
>  key3=value3
> }
> block2
> {
>  key1=value4
>  key2=value5
>  key4=value6
> }
> ...
> blockn
> {
>  key1=value7
>  key2=value8
>  keyn=valuen
> }
> +++++++++++++++++++++++++++++++++++++++
> Different block may have different keys and even same key in different
> blocks may have different values.
>
> Now I want to get a function, which like this:
> func(key)
> and it will return a dictionary as below:
> func(key1) = [block1:value1,block2:value4,...,blockn:value7]
> and if one block has no "key1" parameter, it will not include in this
> dict.
>
> Thanks a lot!

pyparsing should be able to make pretty short work of exactly the
format you want.

FWIW, the creator of the many .ini format(s), Microsoft, no longer
recommends using .ini files.



More information about the Python-list mailing list