extract to dictionaries

Emile van Sebille emile at fenx.com
Fri May 29 11:49:57 EDT 2009


On 5/28/2009 4:03 PM Marius Retegan said...
> 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.
> 
> I woud appreciate any help that could help me solve this.
> Thank you
> 

Assuming you've read the above into paramFile...

for dictvals in [xx.split() for xx in paramFile.split("end") if xx]:
     locals()[dictvals[0]]=dict(zip(dictvals[1::2],dictvals[2::2]))

You-can't-really-call-this-helping-ly yrs,

Emile




More information about the Python-list mailing list