Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing bad format of number and currency from user input to float number

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading