开发者

Testing Python console programs with Unicode strings in NetBeans 6.9

开发者 https://www.devze.com 2023-01-11 01:03 出处:网络
I try to run the following simple code in NetBeans 6.9 s = u\"\\u00B0 Celsius\" print u\"{0}\".format(s)

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:

UnicodeEn开发者_如何学运维codeError: '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')

0

精彩评论

暂无评论...
验证码 换一张
取 消