Tristan T Tristan T - 1 month ago 4x
Python Question

best way to get an integer from string without using regex

I would like to get some integers from a string (the 3rd one). Preferable without using regex.

I saw a lot of stuff.

my string:

xp = '93% (9774/10500)'

So i would like the code to return a list with integers from a string. So the desired output would be:
[93, 9774, 10500]

Some stuff like this doesn't work:

>>> new = [int(s) for s in xp.split() if s.isdigit()]
>>> print new
>>> int(filter(str.isdigit, xp))


Since the problem is that you have to split on different chars, you can first replace everything that's not a digit by a space then split, a one-liner would be :

 xp = '93% (9774/10500)'
 ''.join([ x if x.isdigit() else ' ' for x in xp ]).split() # ['93', '9774', '10500']