easy way to remove nonprintable chars from string

Terry Reedy tjreedy at udel.edu
Thu Jul 24 16:47:07 EDT 2003


"Don Hiatt" <donhiatt at acm.org> wrote in message
news:2b012c5e.0307241138.177e5c8c at posting.google.com...
> Is there an easy way to remove multiple non-printable
> (e.g. "not strings.printable") from a string? Perhaps
> something like foo.replace(list_of_nonprintables, '')
> if it only existed? :-)


>>> help(str.translate)
translate(...)
    S.translate(table [,deletechars]) -> string

    Return a copy of the string S, where all characters occurring
    in the optional argument deletechars are removed, and the
    remaining characters have been mapped through the given
    translation table, which must be a string of length 256.

>>> s_identity=''.join([chr(i) for i in range(256)]) # the 'table' you
need

Terry J. Reedy







More information about the Python-list mailing list