C's isprint() concept?

Jeff Pinyan jeffp at crusoe.net
Sun Aug 15 17:18:39 EDT 1999


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

In Perl, I could do it this way:
  $string =~ tr/\x00-\x1f\x80-\xff//d;

What that means is this:
  remove the following characters from $string:
    characters whose ASCII value is from \x00 (0) to \x1f (31)
    characters whose ASCII value is from \x80 (128) to \xff (255)

This can be done, almost as painlessly, in Python.

--
jeff pinyan    japhy at pobox.com  japhy+perl at pobox.com  japhy+crap at pobox.com
japhy's little hole in the (fire) wall:       http://www.pobox.com/~japhy/
japhy's perl supposit^Wrepository:       http://www.pobox.com/~japhy/perl/
The "CRAP" Project:                 http://www.pobox.com/~japhy/perl/crap/
CPAN ID: PINYAN           http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/





More information about the Python-list mailing list