UnicodeEncodeError: 'ascii' codec can't encode character [...] UnicodeEncodeError: 'ascii' codec can't encode character [...] python python

UnicodeEncodeError: 'ascii' codec can't encode character [...]


I finally solved it. The problem was (I am not sure why) that if you called either __str__() or __repr__() directly it would be hapyp to handle it well, but printing it directly (as in: print obj) does not work (although it should only just call __str__() itself).

The final help came from this article. I already got to the step where I got it to print to the console (but a wrong letter) when I used utf-8 encoding. Finally solved it to be perfectly correct by defining this:

def __str__(self):    return self.__repr__().encode(stdout.encoding)

Now the only open question that remains is: Why do print obj.__str__() and print obj differently with this? It does make no sense to me. And yes, to stress that again: Calling the former or __repr__() DID work. And still does with the explicit encoding.