tcl list to python list?

Paul McGuire ptmcg at austin.rr._bogus_.com
Sun Sep 17 01:01:22 EDT 2006


<jerry.levan at gmail.com> wrote in message 
news:1158459542.693284.98090 at h48g2000cwc.googlegroups.com...
> 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"(),.+=`~!@#$%^&*-|\?/><;:") ) | 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





More information about the Python-list mailing list