Say, I have a (German) expression which reads 10.401,40 (in Mio EUR)
, I'd like to convert this to a real float (in this case around 10 billions) in Python.
This is what I have thus far:
import re, locale
from locale import *
locale.setlocale(locale.LC_ALL, 'de_DE')
string = "10.401,40 (in Mio EUR)"
m = re.search(r'([\d.,]+)', string)
if m is not None:
number = atof(m.group(1)) * 10**6
However, it raises a ValueError
(ValueError: invalid literal for float(): 10.401.40
).
Why? Isn't the .setlocale()
directive supposed to be handling exactly this? Is there a pythonic way that I am (yet!) unaware of?