Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:字典更新序列元素#0的长度为3;2是必需的:NLTK_Python_Python 3.x_Dictionary_Nltk - Fatal编程技术网

Python ValueError:字典更新序列元素#0的长度为3;2是必需的: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) 退换商品 这显然是字节,我想把这个字节数组转换成字

我正在研究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)
退换商品 这显然是字节,我想把这个字节数组转换成字典来访问键“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"])