mikeyy
mikeyy

Reputation: 161

Need help parsing config data

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

Upvotes: 2

Views: 116

Answers (2)

johnsyweb
johnsyweb

Reputation: 141780

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

Upvotes: 3

Chris
Chris

Reputation: 46306

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.

Upvotes: 2

Related Questions