Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parsing equations

Reply
Thread Tools

parsing equations

 
 
munehiro
Guest
Posts: n/a
 
      12-09-2003
hi all,

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.

many thanks

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-09-2003
> 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


Regards,

Diez
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-09-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
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-09-2003
> 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"...


Yup - should have mentioned that.

Diez
 
Reply With Quote
 
munehiro
Guest
Posts: n/a
 
      12-09-2003
Diez B. Roggisch wrote:
> Use the builtin function eval to evaluate an arbitrary python

expression:
>
>
>>>>eval("3+2*6")



many thanks... it's exactly what i need

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show math equations in a GUI stacey Java 6 02-09-2007 08:32 PM
Code for linear congruences, diophantine linear equations alessandra_cabrini@virgilio.it Java 1 11-15-2005 12:23 PM
design boolean equations Hendrik Greving VHDL 2 06-17-2005 05:14 AM
combining several lambda equations Paddy McCarthy Python 7 02-21-2005 06:44 PM
VHDL and extracing equations buke2 VHDL 2 07-28-2004 02:14 PM



Advertisments