简体   繁体   English

在NetBeans 6.9中使用Unicode字符串测试Python控制台程序

[英]Testing Python console programs with Unicode strings in NetBeans 6.9

I try to run the following simple code in NetBeans 6.9 我尝试在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. NetBeans的控制台显然没有正确设置以处理打印非ASCII Unicode字符串。

In general, you should avoid printing unicode strings without explicitly encoding them (eg u_str.encode(some_codec ) first. 通常,应避免在未显式编码unicode字符串的情况下打印unicode字符串(例如u_str.encode(some_codec )首先。

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. 您获得了要编码的unicode字符串。 Assuming that you want UTF-8 encoding use: 假设您要使用UTF-8编码:

s.encode('utf-8') s.encode( 'UTF-8')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM