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

The Archive Base Latest Questions

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

I’ve been given some strings to work with. Each one represents a data set

  • 0

I’ve been given some strings to work with. Each one represents a data set and consists of the data set’s name and the associated statistics. They all have the following form:

s = "| 'TOMATOES_PICKED'       |   914 |   1397 |"

I’m trying to implement a function that will parse the string and return the name of the data set, the first number, and the second number. There are lots of these strings and each one has a different name and associated stats so I’ve figured the best way to do this is with regular expressions. Here’s what I have so far:

def extract_data2(s):
    import re
    name = re.search("'(.*?)'", s).group(1)
    n1 = re.search('\|(.*)\|', s)
    return name, n1

So I’ve done a bit of reading on regular expressions and figured out how to return the name. For each of the strings that I’m working with, the name of the data set is bounded by ‘ ‘ so that’s how I found the name. That part works fine. My problem is with getting the numbers.

What I’m thinking right now is to try to match a pattern that is preceded by a vertical bar (|), then anything (which is why I used .*), and followed by another vertical bar to try to get the first number. Does anyone know how I can do this in Python?

What I tried in the above code for the first number returns basically the whole string as my output, whereas I want to get just the number.

The idea is that it will be able to:

return name, n1, n2

so that when the user inputs a string, it can just parse up the string and return the important information. I’ve noticed in my attempts to get the numbers so far that it will return the number as a string. Is there anyway to return n1 or n2 as just a number? Note that for some of the strings n1 and n2 could be either integers or have a decimal.

I am very new to programming so I apologize if this question seems rudimentary, but I have been reading and searching quite diligently for answers that are close to my case with no luck.

  • 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-23T01:47:27+00:00Added an answer on May 23, 2026 at 1:47 am

    I would use a single regular expression to match the entire line, with the parts I want in named groups ((?P<name>exampl*e)).

    import re
    def extract_data2(s):
        pattern = re.compile(r"""\|\s*                 # opening bar and whitespace
                                 '(?P<name>.*?)'       # quoted name
                                 \s*\|\s*(?P<n1>.*?)   # whitespace, next bar, n1
                                 \s*\|\s*(?P<n2>.*?)   # whitespace, next bar, n2
                                 \s*\|""", re.VERBOSE)
        match = pattern.match(s)
        
        name = match.group("name")
        n1 = float(match.group("n1"))
        n2 = float(match.group("n2"))
        
        return (name, n1, n2)
    

    To convert n1 and n2 from strings to numbers, I use the float function. (If they were only integers, I would use the int function.)

    I used the re.VERBOSE flag and raw multiline strings (r"""...""") to make the regex easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.