0

I have the following string :'Circa 54.000.000 risultati (0,54 secondi)'

I want to get only the second number (54.000.000) as a float (or int so I can use this value to determinate if it is higher than a given number).

result=wd.find_element_by_id('result-stats')
search=result.text.replace("risultati","")
search= search.replace("Circa", "")
search= search.replace("secondi","")

The result is used to take the element from the html and by using .replace I manage to have the following string:'54.000.000 (0,54)'.

From there how can I get 54.000.000 as a number?

2
  • 3
    CAPS LOCK is "internet language" for SHOUTING. Please don't shout. Instead, replace your all-caps title with a title that actually describes your question; for example "extracting number from string" Commented Jan 3, 2022 at 11:31
  • take a loog at .split() Commented Jan 3, 2022 at 11:35

3 Answers 3

1

You can use regex, first you need remove . and replace , to ..

import re 

s = 'Circa 54.000.000 risultati (0,54 secondi)'
s = s.replace(".","").replace(",",".")
# s = 'Circa 54000000 risultati (0.54 secondi)' 

a = re.findall(r'[0-9][0-9,.]+', s)
print(a)
# ['54000000', '0.54']
num = int(a[0])
# 54000000
Sign up to request clarification or add additional context in comments.

Comments

1
import re

text = 'Circa 54.000.000 risultati (0,54 secondi)'
pattern = r'\d+\.\d+\.\d+'

res = re.findall(pattern,text)
convert_to_int = ''.join(res).replace('.','')
print(int(convert_to_int))

Comments

0
>>> string = "54.000.000 (0,54)"
>>> first_num = string.split()[0]
>>> first_num
'54.000.000'
>>> first_num = first_num.replace(".", "")
>>> first_num = int(first_num)
>>> first_num
54000000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.