Is there a simple way to parse this string ?

Paul McGuire ptmcg at austin.rr.com
Wed Dec 19 20:44:30 EST 2007


On Dec 19, 4:23 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Dec 20, 9:10 am, Larry Bates <larry.ba... at websafe.com> wrote:
>
>
>
>
>
> > Stef Mientki wrote:
> > > hello,
>
> > > I need to translate the following string
> > >    a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
> > > into the following list or tuple
> > >    b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>
> > > Is there a simple way to to this.
> > > (Not needed now, but might need it in the future: even deeper nested
> > > lists, represented by a string.)
>
> > > thanks,
> > > Stef Mientki
>
> > There are threads on this list about eval and how you need to be careful.
>
> In particular Paul Maguire recently pointed to a safe evaluator that
> was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
> string/etc constants -- looks like just what you need.
>

I think the last thread of this nature also cited a similar tool by
the effbot, which he describes here: http://www.effbot.org/zone/simple-iterator-parser.htm.
This parser is about 10X faster than the equivalent pyparsing parser.

-- Paul (McGuire)



More information about the Python-list mailing list