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)))
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.
parsed_list = [item for item in x.split(' ')[0] if item.isdigit()]
print(''.join(parsed_list))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.