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

前端 未结 1 1249
感情败类
感情败类 2021-02-02 15:45

I have read the HOWTO on Unicode from the official docs and a full, very detailed article as well. Still I don\'t get it why it throws me this error.

Here is what I atte

相关标签:
1条回答
  • 2021-02-02 16:24

    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.

    0 讨论(0)
提交回复
热议问题