I try to run the following simple code in NetBeans 6.9
s = u"\u00B0 Celsius"
print u"{0}".format(s)
But I get the following error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 0: ordinal not in range(128)
I try to run the following simple code in NetBeans 6.9
s = u"\u00B0 Celsius"
print u"{0}".format(s)
But I get the following error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 0: ordinal not in range(128)
NetBeans's console apparently isn't properly set up to handle printing non-ASCII unicode strings.
In general, you should avoid printing unicode strings without explicitly encoding them (e.g. u_str.encode(some_codec
) first.
In your specific case, you can probably just get away with:
print u'{0}'.format(s).encode('utf-8')
You got a unicode string that you want to encode. Assuming that you want UTF-8 encoding use:
s.encode('utf-8')