1

I have the following code:

u"[%s] - %s" % (a, b.encode('utf-8'))

Where a is a unicode string and b is a classical str

Now this gives the classical error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 2: ordinal not in range(128)

(Position 2 belongs to a)

Why doesn't this work? I thought that if I make string b an unicode string, then it should n't have to convert to ascii when joining the strings together?

1
  • 6
    you might mean b.decode("utf-8"), to convert b bytestring to Unicode. In general, bytes -> decode() -> Unicode -> encode() -> bytes. Commented Jun 23, 2013 at 3:01

1 Answer 1

1

This question was solved, basically I'm an idiot who doesn't know the difference between overloading the __str__() method and the __Unicode__() method. The whole error ocurred when everything was converted to str before it was returned from the method.

Leaving this answer here in case someone gets a similar problem and doesn't know about the __unicode__() magic method of classes.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder where the unicode is in the context of the question
It's basically like this: def __str__(self): return u"[%s] - %s" % (a, b.encode('utf-8')), just replace __str__ with _unicode__ and everything works..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.