easy way to remove nonprintable chars from string

Discussion in 'Python' started by Don Hiatt, Jul 24, 2003.

  1. Don Hiatt

    Don Hiatt Guest

    Greetings,

    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? :)

    Cheers,

    don
     
    Don Hiatt, Jul 24, 2003
    #1
    1. Advertising

  2. Don> Is there an easy way to remove multiple non-printable (e.g. "not
    Don> strings.printable") from a string? Perhaps something like
    Don> foo.replace(list_of_nonprintables, '') if it only existed? :)

    Check out the string module's translate function.

    Skip
     
    Skip Montanaro, Jul 24, 2003
    #2
    1. Advertising

  3. Don Hiatt

    Isaac Raway Guest

    This seems to work. Not sure how fast it'd be, though.

    def stripNoPrint(str):
    results = ""
    for char in str:
    if string.printable.find(char):
    results += char
    return results

    Don Hiatt wrote:

    >Greetings,
    >
    >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? :)
    >
    >Cheers,
    >
    >don
    >
    >
     
    Isaac Raway, Jul 24, 2003
    #3
  4. Don Hiatt

    Terry Reedy Guest

    "Don Hiatt" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Jul 24, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    472
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,292
    Tim Rentsch
    Sep 23, 2005
  3. Hongyu
    Replies:
    9
    Views:
    913
    James Kanze
    Aug 8, 2008
  4. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    231
    Dan Rogers
    Nov 16, 2004
  5. Replies:
    1
    Views:
    180
    Robert Klemme
    May 31, 2006
Loading...

Share This Page