'String manipulation in a Python program
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?
Solution 1:[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))
Solution 2:[2]
>>> 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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jafar Isbarov |
