-3

enter image description hereI got some data from requests-html module and want to do calculation with it but won't work. It is a 'str' type and I tried converting it to int by using int() neither do float() work. Thanks in advance.

hadi tedi
  • 445
  • 1
  • 4
  • 10

3 Answers3

1

first replace the ","

price1bed = price1bed.replace(",","")

then parse to a float

price1bed_float = float(price1bed)

lastly to int (if you want)

price1bed_int = int(price1bed_float)
can
  • 399
  • 5
  • 14
0

Since you have a non numeric character in your string "1,656.00" - specifically the "," - float() and int() are failing to convert. So all you have to do is take out the ",".

You could replace it like this:

s = "1,656.00"
s = s.replace(',','')
s_float = float(s)

That's what I'd do

savvamadar
  • 178
  • 11
0

As @ Pythonista said, you need to remove ","

 price1bed=int(price1bed.replace(',',''))
Bharath
  • 63
  • 1
  • 10