Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:47:47+00:00 2026-05-28T01:47:47+00:00

I have this following piece of code: all_messages = {} num = None index

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:47:48+00:00Added an answer on May 28, 2026 at 1:47 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code from a book. There is this function
I have the following piece of code: if (SqlConnection.State == ConnectionState.Open) { using (SqlDataAdapter
I have this following code, here CssClass is of the String type. <asp:Panel ID=Panel1
I have this following code which will return all the current semesters. How do
Never thought I'd have this problem :) The following snippet of code works in
I have the following piece of code in my LINQ: where (tf.Shipped || tf.Ordered
I have the following piece of code which doesn't compile when I try to
I have the following piece of code which is not working the way I
Win-XP / Excel 2003 / VBA .... I have the following piece of code
I currently have the following piece of code NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.