Python:字典中的值不会设置/upd

2024-10-04 11:31:02 发布

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

这是我使用的函数,奇怪的是,当代码使用“pct\u data”(从tsv/csv文件读取)中的数据时,值不会被覆盖

def get_percentages(objectcode, rp, af):
    cc_pct = {'PCT_MAT': 0
        , 'PCT_IUB': 0
        ,'PCT_UTV': 0
        ,'PCT_OWB': 0
        }
    if pct_data:
        print >> sys.stderr, lineno()
        # check OBJECTCODE
        for row in pct_data[:]:
            if( row['OBJECTCODE'] == objectcode ):
                print >> sys.stderr, lineno(), row
                cc_pct['PCT_MAT'] = row['PCT_MAT']
                cc_pct['PCT_IUB'] = row['PCT_IUB']
                cc_pct['PCT_UTV'] = row['PCT_UTV']
                cc_pct['PCT_OWB'] = row['PCT_OWB']
                print >> sys.stderr, lineno(), "%s %s %s %s" % (parse_float( row['PCT_MAT'].replace('-','0') ), parse_float( row['PCT_IUB'].replace('-','0') ), parse_float( row['PCT_UTV'].replace('-','0') ), parse_float( row['PCT_OWB'].replace('-','0') ) )
            else:
                for compensation, percentage in rp.items():
                    percentage = parse_float(percentage)
                    if ( 'ontw+beg/uitv' in compensation ):
                        cc_pct['PCT_UTV'] = percentage
                        cc_pct['PCT_OWB'] = percentage
                    if ( 'alle kosten' in compensation ):
                        cc_pct['PCT_MAT'] = percentage
                        cc_pct['PCT_IUB'] = percentage
                        cc_pct['PCT_UTV'] = percentage
                        cc_pct['PCT_OWB'] = percentage
                    if ( 'afbouw' in compensation or 'om niet' in compensation ):
                        cc_pct['PCT_MAT'] = (percentage * af) / 100
                        cc_pct['PCT_IUB'] = (percentage * af) / 100
                        cc_pct['PCT_UTV'] = (percentage * af) / 100
                        cc_pct['PCT_OWB'] = (percentage * af) / 100
    else:
        print >> sys.stderr, lineno()
        for compensation, percentage in rp.items():
            percentage = parse_float(percentage)
            if ( 'ontw+beg/uitv' in compensation ):
                cc_pct['PCT_UTV'] = percentage
                cc_pct['PCT_OWB'] = percentage
            if ( 'alle kosten' in compensation ):
                cc_pct['PCT_MAT'] = percentage
                cc_pct['PCT_IUB'] = percentage
                cc_pct['PCT_UTV'] = percentage
                cc_pct['PCT_OWB'] = percentage
            if ( 'afbouw' in compensation or 'om niet' in compensation ):
                cc_pct['PCT_MAT'] = (percentage * af) / 100
                cc_pct['PCT_IUB'] = (percentage * af) / 100
                cc_pct['PCT_UTV'] = (percentage * af) / 100
                cc_pct['PCT_OWB'] = (percentage * af) / 100
    if( objectcode == "HSTEN.0005" ):
        print >> sys.stderr, lineno(), cc_pct
    return cc_pct

打印结果

549
553 {'OBJECTCODE': 'HSTEN.0005', 'PCT_MAT': '46.00', 'PCT_OWB': '45.99', 'PCT_UTV': '45.99', 'PCT_IUB': '46.00'}
558
559 46.0 46.0 45.99 45.99
594 {'PCT_UTV': 0.0, 'PCT_MAT': 0.0, 'PCT_OWB': 0.0, 'PCT_IUB': 0.0}

为什么“cc\u pct”的键值不会更改为“553”行上显示的值。我试过几种方法,但都不管用。你知道吗


Tags: inifparsefloatrowccafpercentage
1条回答
网友
1楼 · 发布于 2024-10-04 11:31:02

在评论中解决:
在进入if( row['OBJECTCODE'] == objectcode ):块并更改cc_pct之后,在循环的下一次迭代中,它可能进入else:块并将cc_pct中的所有值设置为percentage,这似乎是0。–绘图

我只需要在if( row['OBJECTCODE'] == objectcode ):语句中添加一个break。C.凝胶

将其用作pct_dat

pct_data = [{'OBJECTCODE': 'HSTEN.0005', 'PCT_MAT': '46.00', 'PCT_OWB': '45.99', 'PCT_UTV': '45.99', 'PCT_IUB': '46.00'},]

呼叫print(get_percentages('HSTEN.0005',None,None))
输出:

{'PCT_MAT': '46.00', 'PCT_UTV': '45.99', 'PCT_OWB': '45.99', 'PCT_IUB': '46.00'}

因为我不知道rp, af不能测试else:部分!你知道吗

除此之外,为什么要将else:部分两次也没有意义?你知道吗

相关问题 更多 >