Skip to content
Advertisement

Converting exponential number from string to float (encoding issue?) Python

I want to calculate with a value in my dataframe, however, this string consists of an exponential number (’10⁻³’). Is this some kind of encoding issue? How can I convert this string into a float (e.g. 10e-3) so that can perform calculations with this value?

(using Python 3.8.8)

Advertisement

Answer

First problem is to convert the Unicode symbols to something easier to work with.

import unidecode
simpler = unidecode.unidecode('10⁻³')

Now you can put an ‘e’ in front of any ‘-‘ or ‘+’:

simpler = simpler.replace('-', 'e-').replace('+', 'e+')

Now you have a format you can give to float.

f = float(simpler)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement