不支持Python解码Unicode [英] Python decoding Unicode is not supported

查看:153
本文介绍了不支持Python解码Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a problem with my encoding in Python. I have tried different methods but I can't seem to find the best way to encode my output to UTF-8.

这是我想做的: / p>

This is what I am trying to do:

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

searchGoogle c $ c> param 。

searchGoogle returns the first Google result for param.

这是我得到的错误:

exceptions.TypeError: decoding Unicode is not supported

如何让Python编码我的输出在UTF-8中以避免此错误?

Does anyone know how I can make Python encode my output in UTF-8 to avoid this error?

推荐答案

看起来像 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())使用相同的编码?

As a side note, your code expects it to return a utf-8 encoded string so what was the point in decoding it (using unicode()) and encoding back (using .encode()) using the same encoding?

这篇关于不支持Python解码Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆