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

时间:2017-03-04 11:00:26

标签: python

这是我使用的函数,奇怪的是当代码使用“pct_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_pct”的键值不会更改为行“553”上显示的值。我尝试了几种方法,但没有一种方法可行。

1 个答案:

答案 0 :(得分:0)

评论意见:
 进入if( row['OBJECTCODE'] == objectcode ):块并更改cc_pct后,在循环的下一次迭代中,它可能会进入else:块,并将cc_pct中的所有值设置为{{ 1}},似乎是percentage。 - Rawing

我只需要在0语句中添加break。 C. Gellings

将其用作if( row['OBJECTCODE'] == objectcode ):

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:部分两次是没有意义的?