Is there a simple way to parse this string ?

Larry Bates larry.bates at websafe.com
Wed Dec 19 17:10:22 EST 2007


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.
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

-Larry



More information about the Python-list mailing list