I run this line to remove everything but the digits from a string.
str = "a111.113ç"
str = re.sub('[^0-9]', '', str)
but perhaps having to ..
import re
might be slowing things down..
is there a more speedy version of this without importing re ?
UPDATE
perhaps i should find out what is inside of "re" and then bring that over to my script by copy pasting the function directly into my script ?
Using filter
and str.isdigit
:
>>> filter(lambda x: x.isdigit(), "a111.113ç")
'111113'
BTW, don't use str
as a variable name. It will shadow a builtin function/type str
.
一种简单的方法是使用filter
:
filter(lambda x: 47 < ord(x) < 58, str)
If you want speed, re
is probably the best choice. Python is not very fast in making iterations so it is best to rely an some library which is dedicated and optimized for this task.
This should be faster than a regex:
>>> ''.join(filter(str.isdigit, '3,14%_56'))
'31456'
Another possibility is to use str.translate() :
>>> delchars = ''.join(chr(i) for i in range(256) if not chr(i).isdigit())
>>> '3,14%_56'.translate(None, delchars)
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.