I need to write a script in Python which will transform bad input from user to float number.
For example
"10,123.20 Kč" to "10123.2"
"10.023,123.45 Kč" to "10023123.45"
"20 743 210.2 Kč" to "20743210.2"
or any other bad input – this is what I’ve come up with.
Kč is Czech koruna.
My thought process was to get rid of any spaces, letters. Then change every comma to dot to make numbers looks like "123.123.456.78" then delete all dots except of last one in a string and then change it to float so it would looks like "123123456.78". But I don’t know how to do it. If you know any faster and easier way to do it, I would like to know.
This is what I have and I’m lost now.
import re
my_list = ['100,30 Kč','10 000,00 Kč', '10,000.00 Kč', '10000 Kč', '32.100,30 Kč', '12.345,678.91 Kč']
for i in my_list:
ws = i.replace("Kč", '')
x = re.sub(',','.', ws).replace(" ","")
print(x)
>Solution :
You could select the find all numerics instead of trying to remove non-numerics
In any case you have to make some assumtpions about the input, here is the code assuming that a final block of two digits in a text with separators is the fractional part.
import re
my_list = ['100,30 Kč','10 000,00 Kč', '10,000.00 Kč', '10000 Kč', '32.100,30 Kč', '12.345,678.91 Kč']
for s in my_list:
parts = list(re.findall('\d+', s))
if len(parts) == 1 or len(parts[-1]) != 2:
parts.append('0')
print(float(''.join(parts[:-1]) + '.' + parts[-1]))