parsing equations

Discussion in 'Python' started by munehiro, Dec 9, 2003.

  1. munehiro

    munehiro Guest

    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
    munehiro, Dec 9, 2003
    #1
    1. Advertising

  2. > 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
    Diez B. Roggisch, Dec 9, 2003
    #2
    1. Advertising

  3. munehiro

    Peter Otten Guest

    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
    Peter Otten, Dec 9, 2003
    #3
  4. > 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
    Diez B. Roggisch, Dec 9, 2003
    #4
  5. munehiro

    munehiro Guest

    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 :)
    munehiro, Dec 9, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buke2
    Replies:
    2
    Views:
    495
    buke2
    Jul 28, 2004
  2. Hendrik Greving

    design boolean equations

    Hendrik Greving, Jun 16, 2005, in forum: VHDL
    Replies:
    2
    Views:
    496
  3. Replies:
    1
    Views:
    1,088
    Roedy Green
    Nov 15, 2005
  4. Paddy McCarthy

    combining several lambda equations

    Paddy McCarthy, Feb 18, 2005, in forum: Python
    Replies:
    7
    Views:
    298
    Steven Bethard
    Feb 21, 2005
  5. stacey
    Replies:
    6
    Views:
    381
    robert maas, see http://tinyurl.com/uh3t
    Feb 9, 2007
Loading...

Share This Page