Python unicode character in __str__
I'm trying to print cards using their suit unicode character and their values. I tried doing to following:
def __str__(self):
return u'\u2660'.encode('utf-8')
like suggested in another thread, but I keep getting errors 开发者_开发问答saying UnicodeEncodeError: ascii, ♠, 0, 1, ordinal not in range(128)
. What can I do to get those suit character to show up when I print a list of cards?
Where does that UnicodeEncodeError
occur exactly? I can think about two possible issues here:
The
UnicodeEncodeError
occurs in you__unicode__
method.Your
__unicode__
method returns a byte string instead of a unicode object and that byte string contains non-ASCII characters.
Do you have a __unicode__
method in your class?
I tried this on the Python console according to the actual data from your comment:
>>> u'\u2660'.encode('utf-8')
'\xe2\x99\xa0'
>>> print '\xe2\x99\xa0'
♠
It seems to work. Could you please try to print the same on your console? Maybe your console encoding is the problem.
Depending on how you have encoded those "suit symbols" into a byte string, you'll need to make the unicode string back for it by mentioning the appropriate codec (for example, thebytestr.decode('latin-1')
if latin-1 is how you encoded it!), before making the utf-8
encoding of that unicode string. Just unicode(something)
uses the default encoding, which is ASCII and therefore totally ignorant of any "suit symbols"!-)
As I said back then (3 months ago), I'd go for implementing __unicode__
instead of __str__
, but that's just a minor issue of simplicity. The core point is, rather: if your byte string includes anything outside of the limited ASCII encoding, you must know what encoding your byte string uses, and decode it back into Unicode by explicitly using that codec!
I ran the same code and got
>>> u'\u2660'.encode('utf-8')
'\xe2\x99\xa0'
>>> print ('\xe2\x99\xa0')
â™
精彩评论