Is there a simple way to parse this string ?

John Machin sjmachin at lexicon.net
Wed Dec 19 17:23:36 EST 2007


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.

> Make sure you know where the string is coming from and can control the
> contents.  If you read if from a user they could type in:
>
> os.system('rm -rf *') or os.system('del *.*')
>
> eval that and it deletes all the files on your disk
>

Does anyone know of a newsreader that can automatically killfile
people who suggest eval without any warnings at all? Or should we let
Darwinian selection take its effect?



More information about the Python-list mailing list