Python ValueError:字典更新序列元素#0的长度为3;2是必需的:NLTK
我正在研究NLTK:Python ValueError:字典更新序列元素#0的长度为3;2是必需的:NLTK,python,python-3.x,dictionary,nltk,Python,Python 3.x,Dictionary,Nltk,我正在研究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) 退换商品 这显然是字节,我想把这个字节数组转换成字
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)
退换商品
这显然是字节,我想把这个字节数组转换成字典来访问键“label”的值
脚本抛出错误,“ValueError:dictionary更新序列元素#0的长度为3;需要2
“只需使用
json
模块将其转换为常规Python字典:
import json
d = json.loads(the_page.decode("utf-8"))
print(d["label"])
希望这有帮助。看起来像JSON。也许可以尝试使用该模块。
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
import json
d = json.loads(the_page.decode("utf-8"))
print(d["label"])