By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,034 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,034 IT Pros & Developers. It's quick & easy.

Question about string.printable and non-printable characters

P: n/a
-----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 ( df****@student.dei.uc.pt )
PGP Public Key: http://student.dei.uc.pt/~dfcruz/pubring.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCNv94L3+DjgQV3LgRAk0yAKDN5lzrXqsVY5zgvZD2X3 oGtWS5IwCcCtsA
qv5d+KGP3n9Gbx0iUm46f/k=
=sw9h
-----END PGP SIGNATURE-----

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
Michael Hoffman schrieb:
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 : Si*************@Bibliothek.Uni-Augsburg.DE
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.