The quickest approach, if you need to perform more than one or two such delete operations (or even one, but a very long line!), Is to rely on the translate string method, although it needs preparation:
>>> import string >>> allchars = ''.join(chr(i) for i in xrange(256)) >>> identity = string.maketrans('', '') >>> nondigits = allchars.translate(identity, string.digits) >>> s = 'abc123def456' >>> s.translate(identity, nondigits) '123456'
The translate method is different and might be easier to use on Unicode strings than on byte strings, btw:
>>> unondig = dict.fromkeys(xrange(65536)) >>> for x in string.digits: del unondig[ord(x)] ... >>> s = u'abc123def456' >>> s.translate(unondig) u'123456'
You might want to use a collation class rather than an actual dict, especially if your Unicode string can contain characters with very large ord values (which will make an excessive dict ;-). For example:
>>> class keeponly(object): ... def __init__(self, keep): ... self.keep = set(ord(c) for c in keep) ... def __getitem__(self, key): ... if key in self.keep: ... return key ... return None ... >>> s.translate(keeponly(string.digits)) u'123456' >>>
Alex Martelli Aug 08 '09 at 17:35 2009-08-08 17:35
source share