Need help parsing config data
<p>I have this following piece of code:</p>

<pre><code>all_messages = {}
num = None
index = None
begin_message = lambda x: re.match(r'^([0-9]+)\: (.+)', x)
with open(filename) as f:
    messages = {}
    message = []
    for line in f:
        m = re.match(r'^\[(.+)\]$', line)
        if m:
            if index:
                messages.update({num: '\n'.join(message)})
                num = None
                all_messages.update({index: messages}) 
            index = m.group(1)
            print index
        elif begin_message(line):
            if num:
                messages.update({num: '\n'.join(message)})
            del message[:]
            num = int(begin_message(line).group(1))
            begin = begin_message(line).group(2).strip()
            if begin:
                message.append(begin)
        else:
            cont = line.strip()
            if cont:
                if num:
                    message.append(cont)
    else:
        end = line.strip()
        if end:
            if num:
                messages.update({num: '\n'.join(message)})
        all_messages.update({index: messages})
print all_messages
</code></pre>

<p>I'm trying to parse out a config file similar to this:</p>

<pre><code>[Message 1]
1: Hello
2: Hi
3: Blah
   Hah

[Message 2]
1: Hi
2: How's it going?
3: Great.
4: Yep
</code></pre>

<p>I grab the index for the content and then each message, everything works except when I try to update the dictionary it seems to replace the beginning message with the message that follows after.</p>

<p>For example I am expecting a dictionary as such:</p>

<pre><code>{ "Message 1":
  { 1: "Hello", 
    2: "Hi",
    3: "Blah\nHah" 
  },
  "Message 2":
  { 1: "Hi",
    2: "How's it going",
    3: "Great.",
    4: "Yep"
  }
}
</code></pre>

<p>But I end up with:</p>

<pre><code>{ "Message 1":
  { 1: "Hi",
    2: "How's it going",
    3: "Great.",
    4: "Yep"
  },
  "Message 2":
  { 1: "Hi",
    2: "How's it going",
    3: "Great.",
    4: "Yep"
  }
}
</code></pre>

<p>Thanks for any help</p>
