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]: <Page: nestoćšđČĆŽĐŠš>
> In [32]: p.name.replace("sto", "š")
> ---------------------------------------------------------------------------
> <type 'exceptions.UnicodeDecodeError'>    Traceback (most recent call
> last)
>
> /home/aljosa/Projects/nn/<ipython console> in <module>()
>
> <type 'exceptions.UnicodeDecodeError'>: '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 "<stdin>", line 1, in <module>
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to