I have a string and I need to evaluate its numerical value, e.g. a='5.1+2.3-1.9'
I tried using type conversion but it's not working. Error "Could not convert string to float".
I have a string and I need to evaluate its numerical value, e.g. a='5.1+2.3-1.9'
I tried using type conversion but it's not working. Error "Could not convert string to float".
You can use eval, but this means that it will evaluate everything what you put in there, even: __import__('os').system('rm -rf /a-path-you-really-care-about') (source).
A better solution was provided in this stackoverflow (SO) post and advises you to use something like numexpr, which are a lot safer.