stripping unwanted chars from string

Alex Martelli aleaxit at yahoo.com
Thu May 4 11:05:42 EDT 2006


Edward Elliott <nobody at 127.0.0.1> wrote:

> I'm looking for the "best" way to strip a large set of chars from a filename
> string (my definition of best usually means succinct and readable).   I
> only want to allow alphanumeric chars, dashes, and periods.  This is what I
> would write in Perl (bless me father, for I have sinned...):
> 
> $filename =~ tr/\w.-//cd, or equivalently 
> $filename =~ s/[^\w.-]//
> 
> I could just use re.sub like the second example, but that's a bit overkill.
> I'm trying to figure out if there's a good way to do the same thing with
> string methods.  string.translate seems to do what I want, the problem is
> specifying the set of chars to remove.  Obviously hardcoding them all is a
> non-starter.

(untested code, but, the general idea shd be correct)...:

class KeepOnly(object):
    allchars = ''.join(chr(i) for i in xrange(256))
    identity = string.maketrans('', '')

    def __init__(self, chars_to_keep):
        self.chars_to_delete = self.allchars.translate(
            self.identity, chars_to_keep)

    def __call__(self, some_string):
        return some_string.translate(self.identity,
            self.chars_to_delete)


Alex



More information about the Python-list mailing list