总结从webform收到的价值?

时间:2017-11-16 03:09:52

标签: python list

下面是使用cherrypy从web表单收到的python的打印件,我想将" asset_cost_ x "的所有值加起来。其中 x 是动态数字,前提是" asset_allocation_capex_ x "等于' on'。

如何做到尽可能整洁?

我目前正在尝试以下代码,但此代码只是一个指示

代码:

# I would like to get python to determone the total number so that can set 
# the (1,19) automatically
for x in range(1,19):
    try:
        i1 = "asset_cost_" + str(x)
        i2 = "asset_allocation_capex_" + str(x)
        i3 = "asset_allocation_opex_" + str(x)
        #kwarg1 = kwargs[i1]
        #kwarg2 = kwargs[i2]
        kwarg3 = kwargs[i3]
        kwarg3i = kwargs.get(i3)
        #if kwarg2 == 'on':
        #   print "Capex"
        if kwarg3i:
            if kwarg3 == 'on':
                print "Opex"
        #print kwarg1
    except:
        print "Unexpected error:", sys.exc_info()[0]

webform打印输出:

{'asset_description_9': u'Operations and Maintenance Manuals / As-Installed Drawings', 'asset_description_8': u'12 Months Defects Liability and Maintenance', 'checkbox-create_f06': u'off', 'old_asset_ d_1': u'', 'WO': u'1', 'asset_description_5': u'Level 4 Works', 'asset_description_4': u'Level 8 Works', 'asset_description_7': u'Pre and Post Works Building 2 Smoke Management System Testing (incl re porting and fire matrix updating)', 'asset_description_6': u'NSW Long Service Levy', 'project_manager': u'contractors', 'asset_desc_8': u'Structural', 'new_asset_s_1': u'', 'pc_date': u'2017-07-30', ' asset_desc_1': u'Structural', 'F06_yes_33': u'on', 'F06_yes_34': u'on', 'asset_desc_4': u'Structural', 'asset_desc_7': u'Project', 'asset_desc_6': u'Service', 'discipline': u'BW', 'new_asset_w_1': u'' , 'new_asset_mo_1': u'', 'new_asset_ams_1': u'', 'outstanding_notaffecting_d_1': u'', 'F06_yes_22': u'on', 'asset_description_1': u'Level 9 Works', 'drawing-n-1': u'', 'drawing-s-1': u'', 'F06_yes_8': u'on', 'F06_yes_9': u'on', 'F06_yes_4': u'on', 'F06_yes_5': u'on', 'new_asset_ma_1': u'', 'F06_yes_7': u'on', 'F06_yes_1': u'on', 'F06_yes_2': u'on', 'F06_yes_3': u'on', 'old_asset_ma_1': u'', 'const ruction_manager': u'Dane Wilson', 'F06_no_48': u'on', 'outstanding_affecting_r_1': u'', 'new_asset_d_1': u'', 'old_asset_mo_1': u'', 'F06_yes_25': u'on', 'F06_yes_24': u'on', 'F06_yes_23': u'on', 'old _asset_s_1': u'', 'F06_yes_21': u'on', 'asset_desc_9': u'Handover', 'outstanding_affecting_date_1': u'', 'F06_yes_39': u'on', 'd_c': u'on', 'F06-c-48': u'', 'F06-c-45': u'', 'F06-c-44': u'', 'F06-c-47 ': u'', 'contractor_list': u'contractors', 'F06-c-41': u'', 'F06-c-40': u'', 'F06-c-43': u'', 'F06-c-42': u'', 'F06_yes_30': u'on', 'F06-c-22': u'', 'F06-c-21': u'', 'F06-c-20': u'', 'F06-c-34': u'', 'outstanding_notaffecting_date_1': u'', 'asset_desc_5': u'Structural', 'F06_na_6': u'on', 'F06_yes_35': u'on', 'asset_cost_19': u'10500.0', 'asset_cost_18': u'104773.76', 'F06_yes_36': u'on', 'asset_c ost_11': u'34100.64', 'asset_cost_10': u'34100.64', 'asset_cost_13': u'10080.0', 'asset_cost_12': u'34100.64', 'asset_cost_15': u'34100.64', 'asset_cost_14': u'34100.64', 'asset_cost_17': u'83279.83', 'asset_cost_16': u'34100.64', 'outstanding_notaffecting_r_1': u'', 'project_description': u'Haymarket - Divestment of Carpark', 'F06_yes_42': u'on', 'F06-c-38': u'', 'F06-c-39': u'', 'F06_na_17': u'o n', 'F06-c-30': u'', 'F06-c-31': u'', 'F06-c-32': u'', 'F06-c-33': u'', 'F06_na_19': u'on', 'F06_na_18': u'on', 'F06-c-36': u'', 'foxtrot': u'off', 'planner': u' Ken Wenham', 'F06_yes_41': u'on', 'F06 _yes_40': u'on', 'F06_yes_43': u'on', 'checkbox-rpc-status': u'off', 'F06_yes_45': u'on', 'F06_yes_44': u'on', 'F06_yes_47': u'on', 'F06_yes_46': u'on', 'outstanding_affecting_d_1': u'', 'asset_alloca tion_capex_18': u'on', 'asset_allocation_capex_19': u'on', 'new_asset_v_1': u'', 'asset_allocation_capex_14': u'on', 'asset_allocation_capex_15': u'on', 'asset_allocation_capex_16': u'on', 'asset_allo cation_capex_17': u'on', 'asset_allocation_capex_10': u'on', 'asset_allocation_capex_11': u'on', 'asset_allocation_capex_12': u'on', 'asset_allocation_capex_13': u'on', 'asset_desc_11': u'Structural', 'asset_desc_10': u'Structural', 'asset_desc_13': u'Structural', 'asset_desc_12': u'Structural', 'asset_desc_15': u'Structural', 'asset_desc_14': u'Structural', 'asset_desc_17': u'Structural', 'asset_ desc_16': u'Structural', 'asset_desc_19': u'Structural', 'asset_desc_18': u'Structural', 'F06-c-29': u'', 'F06-c-28': u'', 'asset_allocation_capex_8': u'on', 'asset_allocation_capex_9': u'on', 'F06-c- 23': u'', 'asset_allocation_capex_7': u'on', 'asset_allocation_capex_4': u'on', 'asset_allocation_capex_5': u'on', 'F06-c-27': u'', 'F06-c-26': u'', 'F06-c-25': u'', 'F06-c-24': u'', 'asset_cost_1': u '34100.64', 'asset_cost_5': u'34100.64', 'asset_cost_4': u'34100.64', 'asset_cost_7': u'6089.44', 'asset_cost_6': u'2070.0', 'asset_cost_9': u'12096.0', 'asset_cost_8': u'13661.76', 'F06-c-35': u'', ' asset_description_15': u'Level 11 Works', 'asset_description_14': u'Level 7 Works', 'asset_description_17': u'Protection Hoarding and Scaffolding', 'asset_description_16': u'Level 3 Works', 'asset_des cription_11': u'Level 2 Works', 'asset_description_10': u'Level 6 Works', 'asset_description_13': u'Pre and Post Works Smoke Exhaust Air Measurement Testing', 'asset_description_12': u'Level 5 Works', 'F06_yes_37': u'on', 'scope': u'klhjk', 'contract_manager': u' Joel Dixon', 'F06-c-37': u'', 'asset_description_19': u'PC Sums Available for Project', 'asset_description_18': u'Level 1 / Gnd / Baseme nt Works', 'address_ID': u'44196', 'Project_Description': u' Divestment of Carpark', 'new_asset_l_1': u'', 'F06_na_32': u'on', 'F06_na_31': u'on', 'outstanding_affecting_n_1': u'1', 'asset_number_6': u' 11230101 ', 'date_form_completed': u'', 'F06-c-18': u'', 'F06-c-19': u'', 'F06-c-16': u'', 'F06-c-17': u'', 'F06-c-14': u'', 'F06-c-15': u'', 'WAMS_conf_folder': u'off', 'F06-c-13': u'', 'F06-c-10' : u'', 'F06-c-11': u'', 'outstanding_notaffecting_n_1': u'1', 'F06_yes_38': u'on', 'asset_allocation_capex_1': u'on', 'asset_number_9': u' 11230101 ', 'asset_number_8': u' 11230101 ', 'F06_na_28': u'o n', 'F06_na_29': u'on', 'F06_na_26': u'on', 'F06_na_27': u'on', 'F06_na_20': u'on', 'site': u'Haymarket ', 'asset_number_5': u' 11230101 ', 'asset_number_4': u' 11230101 ', 'F06_yes_11': u'on', 'F06-c -4': u'', 'F06-c-5': u'', 'F06-c-6': u'', 'old_asset_a_1': u'', 'F06_yes_12': u'on', 'F06-c-1': u'', 'F06-c-2': u'', 'F06-c-3': u'', 'asset_number_7': u' 11230101 ', 'F06-c-8': u'', 'F06-c-9': u'', 'a sset_number_13': u' 11230101 ', 'asset_number_12': u' 11230101 ', 'asset_number_11': u' 11230101 ', 'asset_number_10': u' 11230101 ', 'asset_number_17': u' 11230101 ', 'asset_number_16': u' 11230101 ' , 'asset_number_15': u' 11230101 ', 'asset_number_14': u' 11230101 ', 'asset_number_19': u' 11230101 ', 'asset_number_18': u' 11230101 ', 'asset_allocation_opex_6': u'on', 'F06_yes_16': u'on', 'drawin g-d-1': u'', 'F06-c-12': u'', 'asset_number_1': u' 11230101 ', 'F06_yes_14': u'on', 'F06-c-7': u'', 'F06_yes_15': u'on', 'F06_yes_13': u'on', 'new_asset_c_1': u'', 'F06_yes_10': u'on', 'PM_contact': u '0404 828 433', 'drawing-i-1': u'', 'sp': u'30060686', 'old_asset_l_1': u'', 'checkbox-create_handover': u'off', 'F06-c-46': u''}

1 个答案:

答案 0 :(得分:0)

如果您无法知道asset_cost_x个项目的总数,您可以执行以下操作来查看整个词典:

asset_cost_keys = [key for key in kwargs if key.startswith("asset_cost_")]

sum = 0
for asset_cost_key in asset_cost_keys:
    i = asset_cost_key[11:] # 11 = len("asset_cost_")
    capex = kwargs.get("asset_allocation_capex_{}".format(i))
    cost = kwargs.get(asset_cost_key)

    if capex is not None and cost is not None and capex == "on":
        sum += float(cost))

首先,我们制作一个名为asset_cost_keys的列表,其中包含kwargs中看起来像asset_cost_x的所有键。请注意,它完整地查看kwargs,因此如果此数据可能很大,您可能需要寻找另一种枚举所有成本项的方法。

然后,对于每一个,我们寻找相应的asset_allocation_capex_x。如果定义了该值,并且等于“on”,并且定义了成本值,我们将成本添加到运行总计中。

注意我没有运行这个,所以可能会有拼写错误,但希望这可以得到你所需要的。