C's isprint() concept?
Mike Fletcher
mcfletch at vrtelecom.com
Sun Aug 15 20:29:07 EDT 1999
If I'm not mistaken, this would be the python equivalent...
teststring = '\000\001that\003\004'
import re
NOTPRINTABLE = r'[^ -~]+'
re.sub( NOTPRINTABLE, ' ', teststring )
# replace all sequences of characters
# not in the character range NOTPRINTABLE
or (from the original form):
# note use of octal instead hexadecimal escape codes
# also note use of raw string to prevent null bytes
# in the regex pattern.
NONPRINTABLE = r'[\000-\037\200-\377]+'
re.sub( NONPRINTABLE, ' ', teststring )
# replace all sequences of characters
# in the range NONPRINTABLE
If you were wanting to replace each character with a " ", leave off the plus
character in the range (thereby matching every single character). If you
need something faster for character-by-character replace, check out the
string module's mapping functions.
I hope that helps,
Mike
-----Original Message-----
From: python-list-request at cwi.nl [mailto:python-list-request at cwi.nl]On
Behalf Of Jeff Pinyan
Sent: August 15, 1999 5:29 PM
To: python-list at cwi.nl
Subject: Re: C's isprint() concept?
> $string =~ tr/\x00-\x1f\x80-\xff//d;
Perhaps, more readably:
$string =~ tr/ -~//cd;
the /c means complement (take the opposite of) the list
the /d means delete those characters without a replacement
the list is ' ' through '~', which is the class of printables
More information about the Python-list
mailing list