<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > I have a file that contains a "tcl" list stored as a string. The list > members are > sql commands ex: > { begin { select * from foo > where baz='whatever'} > {select * from gooble } end > { insert into bar values('Tom', 25) } } > > I would like to parse the tcl list into a python list... > > Any suggestions ( I am running Tkinter...) > > Jerry >
This is very similar to the Python list parser that comes with pyparsing. Adapted to your syntax, this looks something like: tcl = """sql commands ex: { begin { select * from foo where baz='whatever'} {select * from gooble } end { insert into bar values('Tom', 25) } }""" from pyparsing import * tcllist = Forward() element = quotedString | Word(alphas,alphanums+"_") | \ Combine(Word(nums) + "." + Optional(Word(nums)) ) | Word(nums) | \ oneOf( list(r"(),[EMAIL PROTECTED]&*-|\?/><;:") ) | Group( '{' + tcllist + '}' ) tcllist << OneOrMore( element ) import pprint pprint.pprint( tcllist.parseString(tcl).asList() ) Giving: ['sql', 'commands', 'ex', ':', ['{', 'begin', ['{', 'select', '*', 'from', 'foo', 'where', 'baz', '=', "'whatever'", '}'], ['{', 'select', '*', 'from', 'gooble', '}'], 'end', ['{', 'insert', 'into', 'bar', 'values', '(', "'Tom'", ',', '25', ')', '}'], '}']] The pyparsing home page is at pyparsing.wikispaces.com. -- Paul -- http://mail.python.org/mailman/listinfo/python-list