2014-04-10 72 views
1

正從一個WebSocket連接的響應時,我總是遇到這樣的錯誤:如何在python中將unicode字符串編碼爲utf-8?

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

也:

print type(data) 
TypeError: 'unicode' object is not callable 

和:

print type(str(data)) 
TypeError: 'unicode' object is not callable 

誰能教我如何編碼數據字符串回到utf-8?

回答

2

您(或您使用的庫,但更可能是您)已覆蓋全局範圍內的變量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 

要編碼Unicode字符串爲UTF-8字節字符串,請撥打.encode('UTF-8')

>>> u'€'.encode('utf-8') 
'\xe2\x82\xac' 
+0

哦,謝謝,我發現它的一個我使用的功能。下次我會小心的。 – noobprog

相關問題