How do I convert arithemtic string (like "2+2") to a number?

Adam DePrince adam at cognitcorp.com
Sun Feb 6 02:06:49 EST 2005


On Sat, 2005-02-05 at 17:11, Michael Hartl wrote:
> Use the eval function:
> 
> >>> eval("30/(6+9)")
> 2
> 
> Michael

Sure, if you trust the source of the string you are evaluating.  If this
is a form submission on your web site, be wary of the nefarious user who
might submit to you:

commands.getoutput( "rm -rf %(HOME)s"%os.environ )

as the "expression" to evaluate.


Adam DePrince 





More information about the Python-list mailing list