Skip to content Skip to sidebar Skip to footer

How To Convert String With Fraction To Float In Python

I'm prepping some data to insert into MySQL. Unfortunately some of the data is in the form 100½. I need to convert this to a float, but obviously float(100½) fails. Is there a m

Solution 1:

>>>float(sum(fractions.Fraction(x) for x in unidecode.unidecode('100½').split()))
100.5

fractions

unidecode

Solution 2:

from unidecode import unidecode
from fractions import Fraction
a = unidecode('100½').split()
print(float(int(a[0])+Fraction(a[1])))

Post a Comment for "How To Convert String With Fraction To Float In Python"