ValueError: too many values to unpack (expected 2) in templates/results.html:13: ValueError
ValueError: too many values to unpack (expected 2) in templates/results.html:13: ValueError
我正在处理文本分类 NLP 问题。我已经训练了模型并使用训练有素的模型进行分类。预测数据是需要进行预测的有序字典。
预测输出:
OrderedDict([('Subject: fwd : next tuesday at ', 'SPAM'), ('Subject: visit may 4 th vince ', 'SPAM'), ('Subject: enjoy media ( ejym ) e', 'SPAM')])
我将其保存为 json 文件,然后将其加载到显示函数中以使用 render_template
进行渲染
with open('labels.json', 'w') as fp:
json.dump(result,fp)
f = open('labels.json')
predict = json.load(f)
return render_template("result.html",predictions=predict)
模板未使用 json 值呈现:错误 -
ValueError: too many values to unpack (expected 2) in templates
测试用例失败:
{% for email, result in predictions %}
E ValueError: too many values to unpack (expected 2)
code/templates/results.html:13: ValueError
labels.json
{"Subject: fwd : next tuesday at ": "SPAM", "Subject: visit may 4 th vince ": "SPAM", "Subject: enjoy media ( ejym ) e": "SPAM"}
您需要在字典上调用 .items()
来创建 (email, result)
:
的元组列表
{% for email, result in predictions.items() %}
编辑:既然你提到模板文件是固定的,你也可以改变模板变量并在那里应用.items()
:
return render_template("result.html",predictions=predict.items())
我正在处理文本分类 NLP 问题。我已经训练了模型并使用训练有素的模型进行分类。预测数据是需要进行预测的有序字典。
预测输出:
OrderedDict([('Subject: fwd : next tuesday at ', 'SPAM'), ('Subject: visit may 4 th vince ', 'SPAM'), ('Subject: enjoy media ( ejym ) e', 'SPAM')])
我将其保存为 json 文件,然后将其加载到显示函数中以使用 render_template
进行渲染with open('labels.json', 'w') as fp:
json.dump(result,fp)
f = open('labels.json')
predict = json.load(f)
return render_template("result.html",predictions=predict)
模板未使用 json 值呈现:错误 -
ValueError: too many values to unpack (expected 2) in templates
测试用例失败:
{% for email, result in predictions %}
E ValueError: too many values to unpack (expected 2)
code/templates/results.html:13: ValueError
labels.json
{"Subject: fwd : next tuesday at ": "SPAM", "Subject: visit may 4 th vince ": "SPAM", "Subject: enjoy media ( ejym ) e": "SPAM"}
您需要在字典上调用 .items()
来创建 (email, result)
:
{% for email, result in predictions.items() %}
编辑:既然你提到模板文件是固定的,你也可以改变模板变量并在那里应用.items()
:
return render_template("result.html",predictions=predict.items())