python 2中的未知输出unicode

时间:2013-01-04 12:25:19

标签: python netbeans unicode python-2.7 netbeans-7.2

我尝试从this code实施NLP UPC research group来检索某些输入词的同义词。 当我运行测试方法时

def test():
    "tests some functions"
    a=wn.get_words(True)
    print  'length of a: ', len(a)
    print 'a[0]: ', a[0].tostring().decode('utf-8')

输出是不知情的编码

length of a:  16043
a[0]:  �����

在相同的代码中,Unicode已经声明为

def _encode(data):
    return data.encode('utf8')

我使用的平台(net beans 7.2.1)配置为支持utf-8编码

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

如果您已将设置配置为处理UTF-8,则无需将字符串解码为Unicode对象。接下来会发生的是Python使用为sys.stdout检测到的当前编码。

尝试解码:

print 'a[0]: ', a[0].tostring()

答案 1 :(得分:0)

谢谢你的回答。我使用了这个命令而且它与我一起工作

print 'a[0]: ', a[0].encode('utf-8')