python re.search() match invalid literal for int()

Viewed 60

I am trying to turn for example 173.4k (in a string) to 173.400. This is my code:

h = "173.4k net worth blabla "

match = re.search('([-+]?\d*\.\d+|\d+)k net worth', h)
if match:
    f = int(match.group(1))*1000
    print(f)

The result I expected was 173.400 but what I get is:

ValueError: invalid literal for int() with base 10: '173.4'

Could someone please explain me why I get this ValueError?

1 Answers
import re
h = "173.4k net worth blabla "

match = re.search('([-+]?\d*\.\d+|\d+)k net worth', h)
if match:
    f = float(match.group(1))*1000
    print(f)
Related