Python解码Unicode不受支持
我在Python中的编码有问题。 我已经尝试了不同的方法,但我似乎无法find将我的输出编码为UTF-8的最佳方式。
这是我想要做的:
result = unicode(google.searchGoogle(param), "utf-8").encode("utf-8")
searchGoogle返回param的第一个Google结果。
这是我得到的错误:
exceptions.TypeError: decoding Unicode is not supported
有谁知道我可以如何使用UTF-8编码我的输出来避免这个错误?
看起来像google.searchGoogle(param)已经返回unicode :
>>> unicode(u'foo', 'utf-8') Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> unicode(u'foo', 'utf-8') TypeError: decoding Unicode is not supported
所以你想要的是:
result = google.searchGoogle(param).encode("utf-8")
作为一个方面说明,你的代码期望它返回一个utf-8编码的string,解码(使用unicode() )和编码(使用.encode() )使用相同的编码是什么点?