views:

32

answers:

2

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)
+2  A: 

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')
Aaron Gallagher
Here's the result: `° Celsius`. This is likely a NetBeans console problem. Doing this directly on the console (or writing directly to a file) doesn't product the extra `Â`.
Kit
Then NetBeans doesn't want its output to be encoded in utf-8. Based on the result you have there, it seems to want ISO-8859-1 (and you can encode to that to see proper results). This is probably configurable, though I don't know NetBeans myself.
Aaron Gallagher
Writing to a file as in `some_file.write(u'{0}'.format(s))` causes the same error, unless I also use `.encode('utf-8')`. Encoding to ISO-8859-1 works well enough, though.
Kit
@Kit: I'd say that you can directly write to `some_file` if you open it with `codecs.open()`, which allows you to specify an encoding. A console works in the same way: the printed (Unicode) string must be encoded before being sent to the terminal.
EOL
+1 @EOL: I tried it in an ordinary console, too. It appears I do need `codecs.open()`. The ordinary console can take care of a direct `print "u{0}".format(s)` without any hitch; whereas the NetBeans console output can't handle `UTF-8` without requiring `.encode()`.
Kit
A: 

You got a unicode string that you want to encode. Assuming that you want UTF-8 encoding use:

s.encode('utf-8')

John P