parsing equations

Peter Otten __peter__ at web.de
Tue Dec 9 10:03:05 EST 2003


Diez B. Roggisch wrote:

>> i have a question.
>> Given the string '3+2*6' i need to parse it and obtain the result, so
>> 15. Does a module for this operation exists? i need to do simple
>> operations, but also on floating point numbers.
> 
> Use the builtin function eval to evaluate an arbitrary python expression:
> 
>>>> eval("3+2*6")
> 15

Be aware that this is powerful:

>>> eval("cleanMyHarddisk()")
'done'

Just to make sure the OP is aware of the inherent danger of the unsuspecting
"arbitrary python expression"...

Peter




More information about the Python-list mailing list