不支持Python解码Unicode

2024-04-30 00:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Python编码有问题。我尝试过不同的方法,但似乎找不到将输出编码为UTF-8的最佳方法。

这就是我要做的:

result = unicode(google.searchGoogle(param), "utf-8").encode("utf-8")

searchGoogle返回param的第一个Google结果。

这是我得到的错误:

exceptions.TypeError: decoding Unicode is not supported

有人知道我如何让Python用UTF-8编码我的输出以避免这个错误吗?


Tags: 方法编码param错误googleunicoderesultutf
1条回答
网友
1楼 · 发布于 2024-04-30 00:30:35

看起来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编码的字符串,那么解码(使用unicode())和使用相同编码(使用.encode())的目的是什么?

相关问题 更多 >