I 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.
Asked
Active
Viewed 126 times
-3

hadi tedi
- 515
- 1
- 4
- 12
-
1Can you provide an [mcve] – Vulpex Apr 05 '19 at 16:14
-
Again me complaining... Posting an Image of your code is not providing an [mcve] Please do not just post an image of your code but write it here. – Vulpex Apr 05 '19 at 16:16
3 Answers
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
- 444
- 6
- 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
- 306
- 2
- 13
0
As @ Pythonista said, you need to remove ","
price1bed=int(price1bed.replace(',',''))

Bharath
- 113
- 1
- 1
- 10