I’ve got a dictionary that has some Unicode characters, and the console I’m using is set to print UTF-8, but when I try to print the key from the dictionary, I’m getting an error.
import sys
print u'\xc9'
print sys.stdout.encoding
unicode_dict = {u'a': 3,
u'b': 4,
u'c': 5,
u'\xc9': 1,
u'\xc8': 2}
for k, v in unicode_dict.iteritems():
print "{0}: {1}".format(k, v)
When I run this code, I get the following error:
É
UTF-8
a: 3
Traceback (most recent call last):
File "unicodetest.py", line 12, in <module>
print "{0}: {1}".format(k, v)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc9' in position 0: ordinal not in range(128)
What is going on here? Why is print giving me that error?
This line:
means you’re trying to convert the string to ASCII, as the error shows. Don’t do that: either keep it in unicode, and get the output to convert it as necessary:
or convert it explicitly using something that supports those characters, probably utf-8: