【问题标题】:Unknown output unicode in python 2python 2中的未知输出unicode
【发布时间】:2013-01-04 12:25:19
【问题描述】:

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

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编码

如何解决这个问题?

【问题讨论】:

  • 使用repr(a[0].tostring()) 而不是a[0].tostring().decode('utf-8') 看看会返回什么。
  • 谢谢你的建议,但还是有同样的问题 :( 。输出是这样的: Traceback (most recent call last): File "AWN.py", line 402, in test print 'a[0]: ', repr(a[0].tostring()) AttributeError: 'unicode' 对象没有属性 'tostring'

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


【解决方案1】:

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

尝试解码:

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

【讨论】:

    【解决方案2】:

    感谢您的回答。我改用了这个命令,它对我有用

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

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多