Python decoding Unicode is not supported

前端 未结 1 1305
孤城傲影
孤城傲影 2021-01-30 08:09

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.

This is what I am t

相关标签:
1条回答
  • 2021-01-30 08:29

    Looks like google.searchGoogle(param) already returns 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
    

    So what you want is:

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

    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?

    0 讨论(0)
提交回复
热议问题