[Tutor] Re: Using special characters in Python

Jmllr891@cs.com Jmllr891@cs.com
Fri Feb 21 23:49:08 2003


I think I may have a solution to this problem. It seems that when you try to
use characters that are not in the unicode range of 128, without first
directly encoding your character or string, something like this happens:

    ch = unichr(256)
    print ch

    Traceback (most recent call last):
        File "<stdin>", line 1, in ?
    UnicodeError: ASCII encoding error: ordinal not in range(128)

I'm not 100% sure what causes this, but I think it is because Python, by
default, only uses the characters normally found in the English language. I
do not know what the character codes are for the particular characters you
are trying to use, but you are able to use them with something like this:

    ch = unichr(code).encode("UTF-8")

        or

    mystring = "mystring %s%s" % (unichr(129), unichr(256))
    mystring = mystring.encode("UTF-8")

Of course, UTF-8 isn't the only available encoding and the above is only a
basic example of what you could encode and how you could encode it. I also
have a basic idea of how you could find the character codes:

    for n in range(256):
        print n, unichr(n).encode("UTF-8")

The above may be a little messy, but at least it gets the job done. You may
have to play with the range a bit if you can't initially find the
character(s) you are looking for.

I hope this helps!
Joshua Miller