Question about string.printable and non-printable characters

Michael Hoffman cam.ac.uk at mh391.invalid
Tue Mar 15 11:08:27 EST 2005


Daniel Alexandre wrote:

> CheckPrintable(self,message):
>     printablemessage = ""
>     for char in message:
>         if char in string.printable: printablemessage = printablemessage 
> + char
>     return printablemessage

That would probably be best written (using Python 2.4) as:

def check_printable(self, message, printable=string.printable):
     return "".join(char for char in message if char in printable)

It would be much more efficient for one thing. And you can change printable to
be whatever you want. Unfortunately, no one knows what letters you want to
define as printable other than you, or what is printable on your codeset.
string.printable is a least-common denominator ASCII set. You can certainly
make it string.printable + "aeioun" (replacing the ASCII letters with their
accented versions in your codeset of course).

Of course I might be proven totally wrong when the i18n heavies weigh in ;)
-- 
Michael Hoffman



More information about the Python-list mailing list