[Tutor] Encoding

Sander Sweers sander.sweers at gmail.com
Wed Mar 3 22:41:21 CET 2010


On 3 March 2010 20:44, Giorgio <anothernetfellow at gmail.com> wrote:
>>>> s = "ciao è ciao"
>>>> print s
> ciao è ciao
>>>> s.encode('utf-8')
> Traceback (most recent call last):
>   File "<pyshell#2>", line 1, in <module>
>     s.encode('utf-8')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5:
> ordinal not in range(128)

It is confusing but once understand how it works it makes sense.

You start with a 8bit string so you will want to *decode* it to unicode string.

>>> s = "ciao è ciao"
>>> us = s.decode('latin-1')
>>> us
u'ciao \xe8 ciao'
>>> us2 = s.decode('iso-8859-1')
>>> us2
u'ciao \xe8 ciao'

Greets
Sander


More information about the Tutor mailing list