不支持Python解码Unicode

2023-12-29

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

这就是我正在尝试做的:

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

searchGoogle返回第一个 Google 结果param.

这是我得到的错误:

exceptions.TypeError: decoding Unicode is not supported

有谁知道如何让 Python 将输出编码为 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编码的字符串那么解码它的意义何在(使用unicode())并编码回来(使用.encode())使用相同的编码?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不支持Python解码Unicode 的相关文章

随机推荐