我需要提示如何解决此错误

时间:2018-08-15 19:19:45

标签: python numpy sqlalchemy

我有此代码:

qcmz = session1.query(func.sum(KCom.c1)).filter(KCom.users_id != current_user.id and KCom.ofv_id == cmz_id).all()
    qcmz1 = np.array(qcmz) + 0. 
    qcmz2 = float(qcmz1[0])
    qcmz3 = float("{0:.2g}".format(qcmz2))

并返回此错误:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask_login/utils.py", line 228, in decorated_view
    return func(*args, **kwargs)
  File "/home/ubuntu/workspace/avb/app.py", line 822, in fcmz
    qcmz1 = np.array(qcmz) + 0. 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

想法是,填充db表时仅在为空时不返回任何错误,我的意思是当由ForeignKey与另一个关联且为空的KCom.c1返回此问题时。

2 个答案:

答案 0 :(得分:1)

在代码中唯一使用+操作数的位置是在以下行:

qcmz1 = np.array(qcmz) + 0. 

qcmz是什么样子,您想通过添加+ 0.完成什么工作?看起来np.array(qcmz)有时是None类型,您不能添加数字。

答案 1 :(得分:0)

现在运行良好:

qcmz = session1.query(func.sum(KCom.c1)).filter(KCom.users_id != current_user.id and KCom.ofv_id == cmz_id).all()
    qcmz0 = np.array(qcmz)
    if qcmz0 == None:
        qcmz3 = 0
    else:
        qcmz1 = qcmz0 + 0. # conversia din list in float cu numpy
        qcmz2 = float(qcmz1[0])
        qcmz3 = float("{0:.2g}".format(qcmz2))