extract to dictionaries

Rhodri James rhodri at wildebst.demon.co.uk
Fri May 29 08:10:47 EDT 2009


On Fri, 29 May 2009 11:44:30 +0100, Marius Retegan  
<marius.s.retegan at gmail.com> wrote:

> Hi,
>
> On Fri, May 29, 2009 at 2:09 AM, Gary Herron  
> <gherron at islandtraining.com>wrote:
>
>> Marius Retegan 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.
>>>
>>> I would appreciate any help that could help me solve this.
>>> Thank you
>>>
>>>
>>
>> This looks like a homework problem.
>
>
> It's not. I'm passed homework age.
>
>
>>   But even if it's not, you are not likely to find someone who is  
>> willing
>> to put more work into this problem than you have.
>> So  why don't you show us what you've tried, and see if someone is  
>> willing
>> to make suggestions or answer specific question about your attempt at a
>> solution?
>>
>
> I don't now if posting a code that gets into a while loop and never stops
> would demonstrate to you that I've tried.

It would have.  At the very least, it would have told us that you've
missed a common idiom.

> Be assured that before posting to
> the list I did try to solve it myself, because I knew that I might get an
> answer like RTFM or similar.

Not posting code (or code snippets at least) makes it more likely that  
you'll
be told to RTFM, you do realise!

> Maybe I'm not smart enough, but I can't make python to start reading  
> after
> the "parameter1" line and stop at the "end" line. That's all I want a  
> small
> piece of pseudocode to do just that.


I'd be tempted to do it like this

dict_of_dicts = {}
current_dict = {}
current_name = "dummy"

f = open(filename)
for line in f:
   # Do something to skip blank lines
   if line == '\n':
     continue

   # A normal 'key value' pair?
   if line.startswith(' '):
     # Yup.  Split apart the key and value,
     # and add them to the current dictionary
     current_dict.update([line.split()])
   elif line == 'end':
     # Wrap up what we've got and save the dictionary
     dict_of_dicts[current_name] = current_dict
     current_name = dummy
     current_dict = {}
   else:
     # New section.  Really ought to whinge if
     # we haven't ended the old section.
     current_name = line.strip()
     current_dict = {}

You can then pull the parameter sets you want out of
dict_of_dicts (you can probably think of a more meaningful
name for it, but I don't know the context you're working in).
In real code I would use regular expressions rather than
`startswith` and the equality because they cope more easily
with tabs, newlines and other 'invisible' whitespace.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list