-1

Hi I want to parse only digit , for example I parse numbers of users sessions last 5 min , userSes = "12342 last 5 min" , I want to parse only 12342 (this number change every 5 min) , but when I parse this data result is 12342 and 5 ( this number is "from last 5 min " 's number) can any one help me ?

x= ('12342 from last 5 min ')
print(''.join(filter(lambda x: x.isdigit(),x)))
RichEdwards
  • 3,423
  • 2
  • 6
  • 22

2 Answers2

0

You can use regex:

import re

x = '12342 from last 5 min '
n_sessions = int(re.findall('^(\d+).*', x)[0])
print(n_sessions)

^(\d+) .* looks for a number (\d+) at the start of the string (^) before the space and everything else (.*).

If your string is consistent

n_sessions = int(x.split()[0])

should be enough.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
0
parsed_list = [item for item in x.split(' ')[0] if item.isdigit()]
print(''.join(parsed_list))
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
Andrei Gurko
  • 178
  • 5