<\/script>')

Remove letters from string

I have lines like "12454v", "346346z". I want to remove all letters from strings.

Re works fine:

import re str='12454v' re.sub('[^0-9]','', str) #return '12454' 

Is there a way to do this without using regular expressions?

+7
source share
4 answers
 >>> ''.join(filter(str.isdigit, '12454v')) '12454' 
+8
source

In python 2, the second argument to the translate method allows you to specify the characters to delete

http://docs.python.org/2/library/stdtypes.html#str.translate

The above example shows that you can use None as a translation table to simply remove characters:

 >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' 

(You can get a list of all ASCII letters from a string module as string.letters .)

Update : Python 3 also has a translate method, although it requires a slightly different setting:

 from string import ascii_letters tr_table = str.maketrans({c:None for c in ascii_letters}) '12345v'.transate(tr_table) 

For the record, using translation tables in Python 2 is much, much faster than the join / filter method:

 >>> timeit("''.join(filter(lambda c:not c.isalpha(), '12454v'))") 2.698641061782837 >>> timeit("''.join(filter(str.isdigit, '12454v'))") 1.9351119995117188 >>> timeit("'12454v'.translate(None, string.letters)", "import string") 0.38182711601257324 

Similarly in Python 3:

 >>> timeit("'12454v'.translate(tr_table)", "import string; tr_table=str.maketrans({c:None for c in string.ascii_letters})") 0.6507143080000333 >>> timeit("''.join(filter(lambda c:not c.isalpha(), '12454v'))") 2.436105844999929 
+4
source

I think you can try this with the .translate method.

 >>> import string >>> str='12454v' >>> str.translate(None, string.letters) '12454' 

There is a very good answer about the .translate method here .

+2
source

This is somewhat less elegant than others because it does not use a specific function and is somewhat more awkward:

 newStr = '' myStr='12454v' for char in myStr: try: newStr += str(int(char)) except ValueError: pass print newStr 

Again, this is not the best way, but I just throw it there. First I converted it to int so that it can check if an integer is. Then I convert it to str so that it can be added to newStr .

In another note, you should not use str as a variable name, because it hides the str() built-in function.

+1
source

All Articles