Search code examples
pythonxmlpython-2.7traceback

Python Reading value from xml and convert it to float


I'm reading a value from an xml file into a dictionary.

The value is:

10**-3

I want to cast it into a float. It should be like:

>> myval = 10**-3
>> print myval
>> print type(myval)
>> 
>> 0.001
>> <type 'float'>

But the value from the xml is a string and i keep getting the error:

>> float(mydict["value"])

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 10**-3

So it is not possible to cast this string into a float, but i need to get it to work.

Is there an easy way to get around this?


Solution

  • You can use eval

    Ex:

    eval(mydict["value"])