error converting string to float in python

1

Hello, I'm having a problem I'm not getting conveter string for float

The program takes the price of a game dps converts to string and positions through the character and I am not able to convert


price =game.find(attrs="col search_price discounted responsive_secondr
price = str(price)
if not price[110] =="<":
    print("Sem Deconto:", price[101:109])
    print("Com Desconto:",float(price[104:109]))
else:
    print("Sem Deconto:", price[101:110])
    print("Com Desconto:",price[104:110])

Result

File "/home/alison/Documents/program/steam/search_steam.py", line 57, in price     
print ("Discount:", float (price [104: 109]))
ValueError: could not convert string to float: '36, 99 '

    
asked by anonymous 11.06.2017 / 23:22

2 answers

1

As I told you in the comment, the problem is that Python does not recognize the comma (,) as a decimal place representation, only the point (.), you can reverse this by using replace , as Fabio replied. It would look like this:

print("Com Desconto:",float(price.replace(",",".")))

See working at Ideone .

    
12.06.2017 / 00:59
0

The String you are reading has the form '36, 99 '(DD, DD), this can not be converted to float by this python function. You need to convert your string to 'DD.DD' (36.99) before converting.

You could do this with python character override functions (such as replace ).

    
12.06.2017 / 00:04