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 7493503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:59:52+00:00 2026-05-29T16:59:52+00:00

I have a file which contains mostly of names mapping to a certain list

  • 0

I have a file which contains mostly of names mapping to a certain list of numbers. I would like to parse this file, and I presume a regular expression would be good here. I have however two problems:

  • The names can consist of either a word, or a word followed by more words, or a word with underscore. A word can also contain the following characters: (/->)

  • There is a comment in the beginning of the file and at some other places which should not be included in the result. The comments are always a line of--- followed by some text followed by another line of ---

So if I have the following file

-----------------------------------
Comment
-----------------------------------
Ignore these lines
-----------------------------------
someVar                        0.0  1.0
some var with spaces           52   93
another var_with_underscores   3
some var with (special->chars) 13  37  95
another char/slash             132  
-----------------------------------
Another comment
-----------------------------------
yet another var               27.3  9

I want to return a dictionary

{"someVar": [0.0, 1.0], 
 "some var with spaces": [52, 93],
 "another var_with_underscores": [3],
 "another char/slash": [132]  
 "some var with (special->chars)": [13, 37, 95],
 "yet another var": [27.3, 9]}

If it’s too much to ask for one question, I would be happy just knowing the regexp.

I’m using Python 2.7.

  • 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-29T16:59:59+00:00Added an answer on May 29, 2026 at 4:59 pm

    This may do what you’re looking for:

    import re
    
    results = {}
    with open('example.txt', 'r') as f:
        for line in f.readlines():
            m = re.match(r'([^\d]+)(.*)', line.strip())
            if m and m.group(2):
                results[m.group(1).strip()] = [float(n) for n in m.group(2).split()]
    

    Which on your updated example give:

    {'some var with (special->chars)': [13.0, 37.0, 95.0]
     'another var_with_underscores': [3.0]
     'some var with spaces': [52.0, 93.0]
     'someVar': [0.0, 1.0]
     'another char/slash': [132.0]
     'yet another var': [27.3, 9.0]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have file which contains some data, like this 2011-01-02 100100 1 2011-01-02 100200
I have a file which contains some sort of database, it's written like this
I have a JSON file which contains a pound symbol in it. This json
I have written xml file which contains html tags as element like <component> <input
I have a file which contains several thousand numbers, each on its own line:
I have a file which contains values like: CA, PA, NY ND, MO, MI
I have a file which contains something like that: 0x0a ,0x63 ,0x31 ,0x38 ,30
I have a file which contains number of records of varying length. What would
Problem I have a file which contains such text (nothing else) 2010-10-05 I would
I have a file which contains sql statements. Now in this file, I want

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.