Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > easy way to remove nonprintable chars from string

Reply
Thread Tools

easy way to remove nonprintable chars from string

 
 
Don Hiatt
Guest
Posts: n/a
 
      07-24-2003
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
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-24-2003

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

 
Reply With Quote
 
 
 
 
Isaac Raway
Guest
Posts: n/a
 
      07-24-2003
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
>
>




 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-24-2003

"Don Hiatt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Easy way to replace all non-alpha numeric chars in a string? deja@homerlex.mailshell.com Ruby 1 05-31-2006 04:18 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM
RE: easy way to remove nonprintable chars from string sismex01@hebmex.com Python 0 07-24-2003 08:11 PM



Advertisments