numeric expression from string?
Diez B. Roggisch
deets at nospam.web.de
Sat Feb 4 09:52:43 EST 2006
Brian Blais schrieb:
> Hello,
>
> I have a string input from the user, and want to parse it to a number,
> and would like to know how to do it. I would like to be able to accept
> arithmetic operations, like:
>
> '5+5'
> '(4+3)*2'
> '5e3/10**3'
>
> I thought of using eval, which will work, but could lead to bad security
> problems (not that it's a big deal in my app, but still...)
>
> string.atof won't do the job. Is there a preferred way of doing this?
No. If you already know about the pro and cons of eval, either go for it
- or if it bothers you, write a parser using pyparsing and evaluate the
expressions yourself.
Regards,
Diez
More information about the Python-list
mailing list