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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:28:35+00:00 2026-06-18T05:28:35+00:00

I am trying to make a parser in python which can achieve two types

  • 0

I am trying to make a parser in python which can achieve two types of functionality

s1 = foo()
s2 = {'k1':v1,'k2':v2}
s3 = [v1,v2,v3...]

I say two types… where first type is usually objects and second types are kind off like variables.
Now First one is easy..

def parse_string_to_command(string):
     if "foo" in string:
         #handle this.
     elif # handle s2 
     elif #condition to handle s3

EDIT 1:
I think I didnt clearly stated what I am trying to achive.

All I am trying to do is follows:

Everything that passes from that function is a string.. some are methods.. while others are variables.
I am just trying to handle them accordingly.

Basically this is what user will be doing

 > params = {"input":"foobar"}
 > foo = Foo(params)

Now, to handle Foo method.. I do this:

   if "Foo" in string:
      tokens = string.split("=")
      # I have tokens [foo,Foo(params)]

   But params is a string now..whereas it is needed to be a dictionary.

Now I know i can just handle it here..inside Foo method.. but in general I want to handle these variable assignments inside parse_string_to_command function

Is it making any sense.

  • 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-06-18T05:28:37+00:00Added an answer on June 18, 2026 at 5:28 am

    You might want to look into using pythons JSON parser for accomplishing your goal.

    >>> import json
    >>> a = json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]')
    >>> a
    [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
    >>> a[0]
    u'foo'
    >>> a[1]
    {u'bar': [u'baz', None, 1.0, 2]}
    >>> a[1]['bar']
    [u'baz', None, 1.0, 2]
    >>>>>> a[1]['bar'][0]
    u'baz'
    

    or if you need it completely custom… with out the intention to write a complete parser for you, I imagine using some regular expressions will help make your job easier.

    >>> import re
    >>> s1 = "foo()"
    >>> s2 = "{'k1':v1,'k2':v2}"
    >>> s3 = "[v1,v2,v3]"
    >>> re.match('(.*?)\(\)',s1).group(1)
    'foo'
    >>> re.findall('\{*(.*?):(.*?)[,\}$]+',s2)
    [("'k1'", 'v1'), ("'k2'", 'v2')]
    >>> re.findall('\[*(.*?)[,\]$]+',s3)
    ['v1', 'v2', 'v3']
    

    To just identify the 3 types of input strings:

    def parse_string_to_command(s):
        if re.match('(.*?\(\))',s):
            print "This is a function"
        elif re.match('^\{.*\}$',s):
            print "This is kind of like variables 1"
        elif re.match('^\[.*\]$',s):
            print "This is kind of like variables 2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple Python-based HTML parser using regular expressions. My problem
i'm trying to make a very simple YACC parser on Pascal language which just
I'm trying to make a simple TCP server using Twisted ,which can do some
I'm trying to make a markdown parser in python, not because it's useful but
First off, I'm new to Scala. I'm trying to make a template parser in
hi there i'm trying to make a simple parser and using lex and yacc.
I am fairly new to iOS development and trying make a simple app which
Im trying to fix a Python script which takes the posts from a specific
I'm trying to make my WSGI server implementation compatible with both Python 2 and
I'm new to Python and am trying to make use of it to parse

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.