0

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".

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

0

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.

Thymen
  • 2,089
  • 1
  • 9
  • 13