Convert string to command..

Diez B. Roggisch deets at nospam.web.de
Thu Oct 18 16:50:20 CEST 2007


Abandoned wrote:

> I want to convert a string to command..
> For example i have a string:
> a="['1']"
> I want to do this list..
> How can i do ?

The correct wording here would be expression. To evaluate expressions, there
is the function eval:

a = eval("['1']")

But beware: if the expression contains some potentially harmful code, it
will be executed. So it is generally considered bad style to use eval.

An example that fails would be 

a = eval("10**2000**2000")

or such thing.

Another alternative is to use parsers like simplejson to extract the
information. This of course only works, if your expressions are valid json.

Diez



More information about the Python-list mailing list