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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:21:34+00:00 2026-05-15T13:21:34+00:00

I have to parse an input string in python and extract certain parts from

  • 0

I have to parse an input string in python and extract certain parts from it.

the format of the string is

(xx,yyy,(aa,bb,...)) // Inner parenthesis can hold one or more characters in it

I want a function to return xx, yyyy and a list containing aa, bb … etc

I can ofcourse do it by trying to split of the parenthesis and stuff but I want to know if there a proper pythonic way of extracting such info from a string

I have this code which works, but is there a better way to do it (without regex)

def processInput(inputStr):
    value = inputStr.strip()[1:-1]
    parts = value.split(',', 2)
    return parts[0], parts[1], (parts[2].strip()[1:-1]).split(',')
  • 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-15T13:21:35+00:00Added an answer on May 15, 2026 at 1:21 pm

    If you’re allergic to REs, you could use pyparsing:

    >>> import pyparsing as p
    >>> ope, clo, com = map(p.Suppress, '(),')
    >>> w = p.Word(p.alphas)
    >>> s = ope + w + com + w + com + ope + p.delimitedList(w) + clo + clo
    >>> x = '(xx,yyy,(aa,bb,cc))'
    >>> list(s.parseString(x))
    ['xx', 'yyy', 'aa', 'bb', 'cc']
    

    pyparsing also makes it easy to control the exact form of results (e.g. by grouping the last 3 items into their own sublist), if you want. But I think the nicest aspect is how natural (depending on how much space you want to devote to it) you can make the “grammar specification” read: an open paren, a word, a comma, a word, a comma, an open paren, a delimited list of words, two closed parentheses (if you find the assignment to s above not so easy to read, I guess it’s my fault for not choosing longer identifiers;-).

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

Sidebar

Related Questions

I have to parse a String that can assume hex values or other non-hex
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
Let's say I have a standard Python string (such as one obtained from raw_input()
I have an input string that's generated as in following example: string.Format(Document {0}, was
I have a Java app while parses input from a spreadsheet. I've added the
I have to parse a String to create a PathSegmentCollection . The string is
I have a lot of archive data in python dict format that I am
I have a code similar to this: public static IEnumerable<T> ParseInput<T>(string input) { var
Currently I have this: function parse_string($string, $variables){ extract($variables); return eval('return '. addcslashes($string, '') .';');
E.g. i might have input string like below: 2012-07-23T03:30:00.000Z or 2012-06-25T13:00:00.000+08:00 For these two

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.