I have a boolean expression in a string. eg. 20 < 30
. Is there a simple way to parse and evaluate this string so it will return True
(in this case).
ast.literal_eval("20 < 30")
does not work.
Is this a user-defined string, or one you're defining?
If it's a string you're creating, you could use eval
(eval("20 < 30")
), but if the string is given by the user, you might want to sanitize it first...
>>> eval("20<30")
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With