parsing equations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • munehiro

    parsing equations

    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

  • Diez B. Roggisch

    #2
    Re: parsing equations

    > i have a question.[color=blue]
    > 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.[/color]

    Use the builtin function eval to evaluate an arbitrary python expression:
    [color=blue][color=green][color=darkred]
    >>> eval("3+2*6")[/color][/color][/color]
    15


    Regards,

    Diez

    Comment

    • Peter Otten

      #3
      Re: parsing equations

      Diez B. Roggisch wrote:
      [color=blue][color=green]
      >> 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.[/color]
      >
      > Use the builtin function eval to evaluate an arbitrary python expression:
      >[color=green][color=darkred]
      >>>> eval("3+2*6")[/color][/color]
      > 15[/color]

      Be aware that this is powerful:
      [color=blue][color=green][color=darkred]
      >>> eval("cleanMyHa rddisk()")[/color][/color][/color]
      'done'

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

      Peter

      Comment

      • Diez B. Roggisch

        #4
        Re: parsing equations

        > Be aware that this is powerful:[color=blue]
        >[color=green][color=darkred]
        >>>> eval("cleanMyHa rddisk()")[/color][/color]
        > 'done'
        >
        > Just to make sure the OP is aware of the inherent danger of the
        > unsuspecting "arbitrary python expression"...[/color]

        Yup - should have mentioned that.

        Diez

        Comment

        • munehiro

          #5
          Re: parsing equations

          Diez B. Roggisch wrote:[color=blue]
          > Use the builtin function eval to evaluate an arbitrary python[/color]
          expression:[color=blue]
          >
          >[color=green][color=darkred]
          >>>>eval("3+2*6 ")[/color][/color][/color]


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

          Comment

          Working...