Question about string.printable and non-printable characters

Discussion in 'Python' started by Daniel Alexandre, Mar 15, 2005.

  1. -----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 ( )
    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-----
     
    Daniel Alexandre, Mar 15, 2005
    #1
    1. Advertising

  2. 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
     
    Michael Hoffman, Mar 15, 2005
    #2
    1. Advertising

  3. 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 : -Augsburg.DE
     
    Sibylle Koczian, Mar 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metaperl
    Replies:
    1
    Views:
    314
    Peter Otten
    Feb 9, 2007
  2. Alex Vinokur

    Printing non-printable characters

    Alex Vinokur, May 18, 2011, in forum: C++
    Replies:
    8
    Views:
    767
    Alf P. Steinbach /Usenet
    May 18, 2011
  3. Michael W. Ryder
    Replies:
    7
    Views:
    146
    Michael W. Ryder
    Apr 21, 2007
  4. Paul Harman

    Line wrapping non-printable characters

    Paul Harman, Apr 25, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    114
    Paul Harman
    Apr 26, 2006
  5. Larry
    Replies:
    19
    Views:
    317
    comp.llang.perl.moderated
    Dec 25, 2007
Loading...

Share This Page