如何在python中将unicode字符串编码为utf-8?

如何在python中将unicode字符串编码为utf-8?,unicode,character-encoding,Unicode,Character Encoding,当从websocket连接获得响应时,我总是遇到此错误: print type(json.dumps(data)) TypeError: 'unicode' object is not callable 此外: 以及: 有人能教我如何将数据字符串编码回utf-8吗?您(或您使用的库,但更可能是您)已经覆盖了全局范围内的变量type 在这里,我以同样的方式打破东西: >>> type(1) <type 'int'> >>> type(u'9

当从websocket连接获得响应时,我总是遇到此错误:

    print type(json.dumps(data))
TypeError: 'unicode' object is not callable
此外:

以及:

有人能教我如何将数据字符串编码回utf-8吗?

您(或您使用的库,但更可能是您)已经覆盖了全局范围内的变量
type

在这里,我以同样的方式打破东西:

>>> type(1)
<type 'int'>
>>> type(u'9')
<type 'unicode'>
>>> type('9')
<type 'str'>
>>> type = u'i'
>>> type(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'unicode' object is not callable

哦,谢谢,我发现它是我使用的功能之一。下次我会小心的。
    print type(str(data))
TypeError: 'unicode' object is not callable
>>> type(1)
<type 'int'>
>>> type(u'9')
<type 'unicode'>
>>> type('9')
<type 'str'>
>>> type = u'i'
>>> type(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'unicode' object is not callable
>>> u'€'.encode('utf-8')
'\xe2\x82\xac'