Re: UnicodeDecodeError when using replace on model field
On Sep 4, 4:00 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote: > In the working case your initial string is a bytestring, in the non-working > case the initial string is unicode. The error comes from trying to replace > into a unicode string a bytestring containing non-ascii chars: > > >>> s = u'asd' > >>> s = s.replace("s", "š") > > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: > ordinal not in range(128) > > One fix it to specify the replacement string as a unicode literal instead of > a bytestring: > > >>> s = s.replace("s", u"š") > >>> print s > > ašd thanks, now i feel like an idiot 8-) Aljosa --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: UnicodeDecodeError when using replace on model field
2008/9/4 Aljosa Mohorovic <[EMAIL PROTECTED]> > > i can't figure out what's the problem (i also used re with same > results). how should i replace a pattern with non-ascii char? > > working as expected: > In [27]: s > Out[27]: 'asd' > In [28]: s = s.replace("s", "š") > In [29]: s > Out[29]: 'a\xc5\xa1d' > > not working on model fields: > In [30]: p = Page.objects.all()[0] > In [31]: p > Out[31]: > In [32]: p.name.replace("sto", "š") > --- > Traceback (most recent call > last) > > /home/aljosa/Projects/nn/ in () > > : 'ascii' codec can't decode > byte 0xc5 in position 0: ordinal not in range(128) > > In the working case your initial string is a bytestring, in the non-working case the initial string is unicode. The error comes from trying to replace into a unicode string a bytestring containing non-ascii chars: >>> s = u'asd' >>> s = s.replace("s", "š") Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128) One fix it to specify the replacement string as a unicode literal instead of a bytestring: >>> s = s.replace("s", u"š") >>> print s ašd Karen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---