2

I have this following piece of 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

I'm trying to parse out a config file similar to this:

[Message 1]
1: Hello
2: Hi
3: Blah
   Hah

[Message 2]
1: Hi
2: How's it going?
3: Great.
4: Yep

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.

For example I am expecting a dictionary as such:

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

But I end up with:

{ "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"
  }
}

Thanks for any help

1
  • 2
    Have you tried using the configparser library? Commented Jan 10, 2012 at 15:02

2 Answers 2

3

You are re-implementing Python's ConfigParser module and I advise you to stop.

>>> import ConfigParser
>>> config = ConfigParser.ConfigParser()
>>> config.read('8805198.cfg')
['8805198.cfg']
>>> d = dict((section, dict(config.items(section))) for section in config.sections())
>>> print d
{'Message 1': {'1': 'Hello', '3': 'Blah\nHah', '2': 'Hi'}, 'Message 2': {'1': 'Hi', '3': 'Great.', '2': "How's it going?", '4': 'Yep'}}
>>> print d['Message 1']
{'1': 'Hello', '3': 'Blah\nHah', '2': 'Hi'}
>>> print d['Message 1']['3']
Blah
Hah
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure what is wrong with the code you posted, but rather than writing your own configuration file parser, you could use ConfigParser, which is part of the standard library. See the very end of the documentation page for an example using ConfigParser.

5 Comments

The ConfigParser doesn't allow me to do what I am aiming for, but thanks for the suggestion.
@mikeyy: What doesn't ConfigParser do that you need?
@Johnysweb Thanks to your code I stand corrected, it is fully capable of doing what I want. Excuse my ignorance.
@mikeyy ConfigParser is very flexible, see the first example on the documentation page: this summarises a number of features. For example, = are : are both used as key-value separators and that ConfigParser supports interpolataion, i.e. values can contain format strings to other values (in the same section). The later point would be time consuming to do yourself.
@Chris You are right. I am implementing ConfigParser at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.