获取ValueError:int()的文本无效,基数为10:“”错误,不知道原因

2024-05-14 19:15:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道以前有人问过这个问题,但就我的情况而言,我似乎不明白为什么要扔这个

当我试图运行我的计算时,我的控制台上会出现以下错误:

ValueError: invalid literal for int() with base 10: ''

它说它来自

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)

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

这是我的ajax.py代码所在的位置:

@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')

Tags: fromimportidjsondatadateamountnow
3条回答

函数的作用是引发一个异常,因为您试图转换不是数字的内容。

我建议您可以考虑使用调试print语句来了解初始计划和fiid是什么以及它们是如何变化的。

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

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

在调用链的某个地方,传递一个空字符串,然后代码试图将其转换为整数。把你的输入记录下来。

建议您查看planfiid的值,因为planfiid没有设置为您认为的值。

相关问题 更多 >

    热门问题