unicode strings?!

Sylvain Thenault syt at gemini.logilab.fr
Thu Nov 8 14:04:25 CET 2001


On Thu, 08 Nov 2001 13:42:48 +0100, Philipp Schmid <schmidp at o000o.org> wrote:
>hi,
>
>can anyone help me with this little example program?
>
>#!/usr/bin/python2
>
>lala = "aslfjaslkjf lasjflasjkfals faslkfj aljfaöljfkalöfjalö jflajf"
>print lala
>lala = u'%s' % lala
>print lala
>
>
>if i run it I get the following error, why?
>
>aslfjaslkjf lasjflasjkfals faslkfj aljfaöljfkalöfjalö jflajf
>Traceback (most recent call last):
>  File "./test.py", line 5, in ?
>    lala = u'%s' % lala
>UnicodeError: UTF-8 decoding error: invalid data

To convert a string to unicode, python supposes your string is encoding 
with UTF-8 if you don't specify encoding. You got this error because your 
string isn't UTF-8 but latin1 (iso-8859-1).

replace line 5 with: lala = unicode(lala, 'ISO-8859-1)
or lala = unicode(lala, 'latin1')
and it should works.

-- 
Sylvain Thenault

  LOGILAB           http://www.logilab.org




More information about the Python-list mailing list