更新后,在dict中保留重复值

时间:2017-11-23 00:58:58

标签: python excel dictionary

我使用以下代码将dict转换为json并将其发送到javascript处理。 代码从excel文件中读取并更新nested_dict。 我的问题是当excel电子表格中有2个相似的值时,显然更新后的dict将忽略之前的值。

我如何能够保留所有重复项,请参阅下面的nested_dict示例。

代码:

book = xlrd.open_workbook(offer_path)
first_sheet = book.sheet_by_index(1)
nested_dict = {}
nested_dict["Design_Lump_Sum"] = {}
#print nested_dict
for i in range(12,19):
    design_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(design_cells[0].value) and str(design_cells[1].value):
        if design_cells[1].value >0 :
            nested_dict["Design_Lump_Sum"].update({str(design_cells[0].value) : str(design_cells[1].value)})


nested_dict["Capex_Lump_Sum"] = {}
for i in range(22,155):
    capex_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(capex_cells[0].value) and str(capex_cells[1].value):
        if capex_cells[1].value >0 : 
            nested_dict["Capex_Lump_Sum"].update({str(capex_cells[0].value) : str(capex_cells[1].value)})

nested_dict["Opex_Lump_Sum"] = {}
for i in range(157,175):
    Opex_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(Opex_cells[0].value) and str(Opex_cells[1].value):
        if Opex_cells[1].value >0 : 
            nested_dict["Opex_Lump_Sum"].update({str(Opex_cells[0].value) : str(Opex_cells[1].value)})

nested_dict["Provisional_Sum"] = {}
for i in range(176,198):
    Provisional_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(Provisional_cells[0].value) and str(Provisional_cells[1].value):
        if Provisional_cells[1].value >0 or Provisional_cells[1].value <0:
            nested_dict["Provisional_Sum"].update({str(Provisional_cells[0].value) : str(Provisional_cells[1].value)})

nested_dict["Management_fees"] = {}
for i in range(200,209):
    Management_fees = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(Management_fees[0].value) and str(Management_fees[1].value):
        if Management_fees[1].value >0 :
            nested_dict["Management_fees"].update({str(Management_fees[0].value) : str(Management_fees[1].value)})

nested_dict["CSA"] = {}
for i in range(211,231):
    CSA = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(CSA[0].value) and str(CSA[1].value):
        if CSA[1].value >0 or CSA[1].value <0 :
            nested_dict["CSA"].update({str(CSA[0].value) : str(CSA[1].value)})

nested_dict["Total"] = {}
nested_dict["Total"].update({"sub_Total": str(first_sheet.cell(234,3))})


print  nested_dict
return json.dumps(nested_dict)

列出由以下代码打印的输出示例:

  

{&#39; Management_fees&#39;:{},&#39; Capex_Lump_Sum&#39;:{&#39;制冷剂管道&#39;:   &#39; 48040.447&#39;,&#39;准​​备拖把&#39;&#39; 2137.681&#39;,&#39;标签&#39;:&#39; 653.016&#39;,   &#39;渗透和保护&#39;:&#39; 1535.534&#39;,&#39; AC Plinth和Trays&#39;:   &#39; 5221.762&#39;,&#39;绝缘面板Windows&#39;:&#39; 6527.794&#39;,&#39; MSSB&#39 ;:   &#39; 19582.199&#39;,&#39;制作好墙和地板&#39;:#41; 4154.696&#39;,&#39;控制&#39;:   &#39; 24092.978&#39;,&#39; **建筑测量师费(杂项)&#39;:&#39; 7038.85&#39;,&#39;项目   管理,监督&#39; 38447.5&#39;,&#39;绘画&#39;:&#39; 9138.675&#39;,   &#39;预备&#39;:&#39; 1306.032&#39;,&#39;包装系统布线&#39;:&#39; 16971.318&#39;,   &#39;保修&#39;:&#39; 2610.881&#39;,&#39; Ductwork,Valves and Dampers&#39;:&#39; 77262.913&#39;,   &#39; Unit ty pe&#39;&#39; 89107.109&#39;,&#39;新单位的结构工程师&#39;:   &#39; 3916.913&#39;,&#39;手册&#39;:&#39; 3823.456&#39;,&#39; AMS和BMS工作&#39;:&#39; 3916.913&#39; ,   &#39; Accoustic Report&#39;&#39; 4243.421&#39;,&#39; Lighting&#39;:&#39; 5033.665&#39;,&#39;图纸&#39;:   &#39; 38   23.456&#39;,&#39; Temp Cooling&#39;:&#39; 10443.524&#39;,&#39;调试&#39;:&#39; 3263.897&#39;,&#39; Crane& #39;:&#39; 3003.637&#39;,&#39;拆除&#39;:&#39; 26455.429&#39;,&#39;火灾探测和   报警FIP修改&#39;&#39; 3263.897&#39;&#39; LSL&#39;:&#39; 1939.81059657&#39;,&#39; Ec   onomy Cycle&#39;:&#39; 23169.055&#39;},&#39; Provisional_Sum&#39;:{&#39;凝汽器屋顶   平台&#39;:&#39; 20000.0&#39;,&#39;去除电缆室出口外的梯子   楼梯&#39;:&#39; 10000.0&#39;},&#39; Opex_Lump_Sum&#39;:{},&#39; CSA&#39;:{&#39;其他亨德利   费用&#39; 3742.5&#39;,&#39;外部阶梯&#39;:&#39; 10147.0&#39;,&#39;石棉清除工程   总计(42,485.15美元)(PS总和下的剩余费用为30,000美元)&#39;:   &#39; 12485.15&#39;},&#39; Design_Lump_Sum&#39;:{&#39; **详细设计&#39;:&#39; 15379.0&#39;,&#39; *   * Const舞台服务&#39;:&#39; 4732.0&#39;,&#39; **初步设计&#39;:&#39; 2366.0&#39;},&#39; Total&#39;: {&#39; sub_Total&#39;:&#39;号码:530154.0105965699&#39;}}

0 个答案:

没有答案