我正在从一个JSON文件中提取数据。
我试图以一种方式打包我的数据,以便预处理脚本可以使用它。
预处理脚本代码:
for key in split:
hist = split[key]
for text, ans, qid in hist:
现在我有一个提取出来的数据集,格式如下所示:Dictionary。
dic{}
result //is the result of removing some formatting elements and stuff from the Question, so is a question string
answer //is the answer for the Q
i // is the counter for Q & A pairs
So I have
this = (result,answer,i)
dic[this]=this
当我尝试复制原始代码时,出现了“值过多无法解包”的错误。
for key in dic:
print(key)
hist = dic[key]
print(hist[0])
print(hist[1])
print(hist[2])
for text, ans, qid in hist[0:2]: // EDIT: changing this to hist[0:3] or hist has no effect
print(text)
输出:
(u'This type of year happens once every four', u'leap', 1175)
This type of year happens once every four
leap
1175
Traceback (most recent call last):
File "pickler.py", line 34, in <module>
for text, ans, qid in hist[0:2]:
ValueError: too many values to unpack
正如您所看到的,我甚至尝试限制赋值的右侧,但那也没有帮助
而且,正如您所看到的,每个项目的输出都与应该匹配的一样
hist[0]=This type of year happens once every four
hist[1]=leap
hist[2]=1175
而 len(hist) 也返回 3。
为什么会这样?具有 hist、hist[:3] 和 hist[0:3] 相同的结果,会导致“要解包的值过多”错误。