获取ValueError:以10为底的int()的无效文字:''错误,不知道为什么 [英] Getting a ValueError: invalid literal for int() with base 10: '' error and don't know why

查看:60
本文介绍了获取ValueError:以10为底的int()的无效文字:''错误,不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经有人问过这个问题,但是就我的情况而言,我似乎无法弄清楚为什么会抛出这个问题

I know this has been asked before, but for my circumstance, I can't seem to figure out why this is being thrown

当我尝试运行计算时,从控制台收到此错误:

When I try to run my calculations, I am given this error from my console:

ValueError:以10为底的int()的无效文字:''

并且说它来自

File "/var/sites/live_mbpathways/moneybroker/apps/investments/ajax.py", line 30, in calc_cdic
    investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)

有人知道为什么会这样吗?

Does anyone know why this could be happening?

这里是该代码所在的我的ajax.py:

Here is my ajax.py where that code is located:

@login_required
@moneybroker_auth
@csrf_exempt
def calc_cdic(request, plan, investment_id=None, *args, **kwargs):
    from investments.models import Investment
    from financial_institutions.models import FinancialInstitution
    from profiles.models import Profile
    from plans.models import Plan
    from datetime import datetime
    now = datetime.now()
    json = {}
    data = request.POST

    if request.is_ajax():
        total = 0
        fiid = data.get('financial_institution')
        amt = data.get('amount') or 0
        pay_amt = data.get('pay_amount') or 0
        mat_amt = data.get('maturity_amount') or 0
        investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by('maturity_date').exclude(id=investment_id)
        for i in investments:
            total += i.maturity_amount
        print total
        json['total'] = float(str(total))
        json['amt'] = float(str(amt))
        json['pay_amt'] = float(str(pay_amt))
        json['mat_amount'] = float(str(mat_amt))
        json['fiid'] = fiid
        print json

    return HttpResponse(simplejson.dumps(json), mimetype='application/json')

推荐答案

int()函数引发异常,因为您尝试转换的不是数字.

The int() function is throwing an exception because you are trying to convert something that is not a number.

我建议您考虑使用调试打印语句来找出最初的计划和fiid以及它们如何更改.

I suggest that you might consider using debugging print statements to find out what plan's and fiid's are initally and how they change.

您可以做的另一件事是使用try/catch包装对int()的调用

Another thing you can do is wrap the call to int() using try/catch

val='a'
try:
    int_val = int(val)
except ValueError:
    print("Failure w/ value " + val)

这篇关于获取ValueError:以10为底的int()的无效文字:''错误,不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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