C's isprint() concept?

Aahz Maruch aahz at netcom.com
Sun Aug 15 17:54:20 EDT 1999

In article <slrn7rea88.9uf.jblaine at shell2.shore.net>,
Jeff Blaine <jblaine at shell2.shore.net> wrote:
>If I want to replace all non-printable characters in a string with a single
>space, what would be the best way?  Do I need to loop over the entire string
>character by character checking the ord() value of each one?  Anyone have
>a sane way to do this with regular expressions?

Oh, sure.  What isn't clear from the way you write this is whether a run
of multiple non-printable characters should be replace with a single
space.  Here's a regex that I created recently as part of some code to
detect binary documents:

   # Everything that isn't CR/LF or 7-bit normal characters
   reBinary = re.compile ( r'[^\r\n\x20-\x7F]' )

To make this substitute a space for each character, just do

   newString = reBinary.sub ( ' ', string )

I leave the multi-sub as an exercise for the reader.

>[ C has isprint() and isgraph() macros ]

Don't remember offhand what isgraph() does, but it occurs to me that it
would be useful to have a character class string.printable.  Tim?  Guido?
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>      http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6  (if you want to know, do some research)

More information about the Python-list mailing list