remove letters from a data extraction leaving only numbers Python

Viewed 33

I have this selenium code

x = browser.find_element_by_xpath('//*[@id="paginacaoSuperior"]/tbody/tr[1]/td[1]/h2/span').text

this code returns this value to me

result 1 to 299

I wanted to remove these values

result 1 to

leaving only 299

with this value 299 divide by 12

    y = 'Result1to'
    for i in range(0,len(x)):
        x = x.replace(y[i],"")
    print(x)

I used this code but gave error

2 Answers

Use str.split with negative index.

Ex:

s = "result 1 to 299"
print(s.split()[-1]) # -->299

Do the following to sove the issue:

s = "result 1 to 299"
print(round(int(s.split()[-1])/12, 4))

# first action is s.split() we devide string and get list
# s.split()[-1] take the last element of the list
# int(s.split()[-1]) convert it to integer (you can use float instead)
# round(int(s.split()[-1])/12, 4) divide by 12 and show only 4 digits after point  
Related