Question about string.printable and non-printable characters

D

Daniel Alexandre

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi there,

I'm using the following method in my program to check whether a message
received is printable or not and to strip the non-printable characters:

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

The method is working fine, except in one detail, it's also stripping
the accented letters which I didn't want to happen. Is there a
string.printable declaration which contains accented letters? Thanks in
advance. If you can, please reply to my email.
- --
Best Regards,
Daniel Alexandre ( (e-mail address removed) )
PGP Public Key: http://student.dei.uc.pt/~dfcruz/pubring.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCNv94L3+DjgQV3LgRAk0yAKDN5lzrXqsVY5zgvZD2X3oGtWS5IwCcCtsA
qv5d+KGP3n9Gbx0iUm46f/k=
=sw9h
-----END PGP SIGNATURE-----
 
M

Michael Hoffman

Daniel said:
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 ;)
 
S

Sibylle Koczian

Michael said:
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).
There is something I don't understand about string.printable: on the one
hand the library reference says "This is a combination of digits,
letters, punctuation, and whitespace." The value of string.letters is
locale-dependent, so string.printable should change as well after
calling locale.setlocale(). But it doesn't.

But you could always call locale.setlocale() and afterwards combine your
"printable" string yourself.

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg

Tel.: (0821) 598-2400, Fax : (0821) 598-2410
e-mail : (e-mail address removed)-Augsburg.DE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top