char 128? no... 256

Erik Max Francis max at alcyone.com
Wed Feb 12 08:45:28 CET 2003


Afanasiy wrote:

> I obviously need to be able to decode the Unicode object.
> The sad part is, this doesn't have have to be a Unicode
> object but Python decides to make it one.

You're the one making the Unicode object in these snippets, so I don't
know how Python is to blame.

> So I guess the
> solution is to stop using Python for this project.

Maybe so (I doubt it), but perhaps you should understand the problem
first.

> >>> s = u'ö'
> >>> s
> u'\x94'
> >>> unicode(s,'latin-1')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: decoding Unicode is not supported

Did you look at my interpreter snippet?  You're taking a Unicode string,
and are trying to convert it to another Unicode string.  You say you
don't want a Unicode string, but you 1. make one with a constant and
then 2. try to make a new one, and finally 3. blaming Python.

Instead use the encode method on the unicode object:

>>> u = u'\x94' # u is a Unicode string
>>> s = u.encode('latin-1') # s is a just plain byte string
>>> s
'\x94'
>>> type(u)
<type 'unicode'>
>>> type(s)
<type 'str'>

See it now?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ When you love somebody, you're always in trouble.
\__/ Col. Sherman Potter
    ZOE / http://www.alcyone.com/pyos/zoe/
 A simple Python OpenGL rendering engine.




More information about the Python-list mailing list