Python(烧瓶/棉花糖)ValueError:太多值无法解包(预期2) [英] Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

查看:306
本文介绍了Python(烧瓶/棉花糖)ValueError:太多值无法解包(预期2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Flask项目,并且正在使用棉花糖来验证用户输入. 下面是一个代码段:

I am working on a Flask project and I am using marshmallow to validate user input. Below is a code snippet:

def create_user():
    in_data = request.get_json()
    data, errors = Userschema.load(in_data)
    if errors:
        return (errors), 400
    fname = data.get('fname')
    lname = data.get('lname')
    email = data.get('email')
    password = data.get('password')
    cpass = data.get('cpass')

当我删除errors部分时,代码可以正常工作.按原样运行时,出现以下错误:

When I eliminate the errors part, the code works perfectly. When I run it as it is, I get the following error:

builtins.ValueError

builtins.ValueError

ValueError:太多值无法解包(预期2)

ValueError: too many values to unpack (expected 2)

回溯(最近通话最近一次)

Traceback (most recent call last)

文件 "/home/..project-details.../venv3/lib/python3.6/site-packages/flask/app.py", 第2000行,在致电

File "/home/..project-details.../venv3/lib/python3.6/site-packages/flask/app.py", line 2000, in call

错误=无

ctx.auto_pop(错误)

ctx.auto_pop(error)

def __call__(self, environ, start_response):
    """Shortcut for :attr:`wsgi_app`."""
    return self.wsgi_app(environ, start_response)


def __repr__(self):
    return '<%s %r>' % (
        self.__class__.__name__,
        self.name,

注意:var in_data是字典. 有什么想法吗?

Note: The var in_data is a dict. Any ideas??

推荐答案

我建议您检查依赖项版本. 根据棉花糖API参考,schema.load返回:

I recommend you check your dependency versions. Per the Marshmallow API reference, schema.load returns:

在3.0.0b7版中进行了更改:此方法返回反序列化的数据,而不是(数据,错误)二元组.如果传递了无效数据,则会引发ValidationError.

Changed in version 3.0.0b7: This method returns the deserialized data rather than a (data, errors) duple. A ValidationError is raised if invalid data are passed.

我怀疑python正在尝试将dict(作为单个对象返回)解压缩为两个变量.引发异常是因为错误"变量中没有任何内容.下面再现了错误:

I suspect python is trying to unpack the dict (returned as a singular object) into two variables. The exception is raised because there is nothing to pack into the 'errors' variable. The below reproduces the error:

d = dict()
d['test'] = 10101
a, b = d
print("%s : %s" % (a, b))

这篇关于Python(烧瓶/棉花糖)ValueError:太多值无法解包(预期2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆