这是我使用的函数,奇怪的是,当代码使用“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”行上显示的值。我试过几种方法,但都不管用。你知道吗
在评论中解决:
在进入
if( row['OBJECTCODE'] == objectcode ):
块并更改cc_pct
之后,在循环的下一次迭代中,它可能进入else:
块并将cc_pct
中的所有值设置为percentage
,这似乎是0
。–绘图我只需要在
if( row['OBJECTCODE'] == objectcode ):
语句中添加一个break
。C.凝胶将其用作
pct_dat
:呼叫
print(get_percentages('HSTEN.0005',None,None))
输出:
因为我不知道
rp, af
不能测试else:
部分!你知道吗除此之外,为什么要将
else:
部分两次也没有意义?你知道吗相关问题 更多 >
编程相关推荐