0
我的工作NLTK:ValueError:字典更新順序元素#0的長度爲3; 2需要:NLTK
data = urllib.parse.urlencode({"text": "I'm good "}).encode('ascii')
u = urllib.request.urlopen("http://text-processing.com/api/sentiment/",data)
the_page = u.read()
print (the_page)
返回
b'{"probability": {"neg": 0.22531846855219551, "neutral":
0.084284385065714951, "pos": 0.77468153144780449}, "label": "pos"}'
這顯然是字節,我想轉換此字節數組字典的鍵「標籤的訪問值「
d = dict(toks.split(":") for toks in the_page.decode("ascii").split(",") if
toks) #Error referred here
for key,value in d.items():
if key is 'label':
print (value)
#Should return pos
腳本拋出錯誤, 」ValueError異常:詞典更新序列元素#0具有長度3; 2需要 「
看起來像JSON。也許嘗試使用['json'](https://docs.python.org/3/library/json.html)模塊。 –