replacement for string.printable

Paul McGuire ptmcg at austin.rr.com
Wed Aug 15 22:09:01 EDT 2007


On Aug 15, 1:56 pm, John K Masters <johnmast... at oxtedonline.net>
wrote:
> >From what I have read the string module is obsolete and should not be
>
> used but I am working on a project that parses printable files created
> in a DOS program and creates a web page for each file. I am using the
> string.printable constant to determine which characters should be kept;
> the files contain many print control codes. There seems to be nothing
> like this in the string methods. isalnum() seems the nearest but gives
> false for '+' '!' etc.
>
> I realise I could define a global string to cover this but wondered if
> there was another, better, way
>
> Regards, John
> --
> War is God's way of teaching Americans geography
> Ambrose Bierce (1842 - 1914)

Well, I was all set with a cocky answer to the effect of "here's half
a dozen different ways to test to see if the ordinal of a character is
between 33 and 127 inclusive," assuming that the "printable" in
string.printable meant "prints something that is not whitespace."
Well, I'm glad I checked - string.printable includes a number of
whitespace characters, including those with ASCII codes 32 (space), 9
(tab), 10 (linefeed), 13 (CR), 11 (FF), and 12 (VT).  So you could
hard code such a string (you are working with a legacy DOS app after
all), or maybe better, make it a set of chars, since it seems that you
are doing massive numbers of membership tests using "if c in string:"
- create the set once, and then use "if c in setMadeFromString:".

-- Paul







More information about the Python-list mailing list