In article <[EMAIL PROTECTED]>, Paul McGuire <[EMAIL PROTECTED]> wrote: ><[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 > >
Noooooooooooooooooo! I'll be more precise: pyparsing is quite wonderful, and I'm all in favor of clever demonstrations of its capabilities. I don't think this is one, though; in fact, although I haven't yet paid enough attention to the original question to provide an example, I'm reasonably certain that the code offered above mishandles at least some data (Tcl syntax is different from what outsiders expect). Paddy's advice elsewhere in this thread is almost entirely correct. Anyone who has imported Tkinter has all of Tcl avail- able immediately, so much the easiest, most reliable, most maintainable, and most lucid solution is simply to iterate in Tcl over the list, and construct thus the corresponding Python list. I'm willing to write either a correct implementation, or a counterexample with problematic data, if these sufficiently interest readers. It'll be a day or two before I can make the time to be careful, though. -- http://mail.python.org/mailman/listinfo/python-list