如何使用Python创建嵌套JSON

如何使用Python创建嵌套JSON,python,json,dictionary,Python,Json,Dictionary,我已经阅读了从csv创建嵌套JSON,但在我的案例中没有帮助 我想使用python从excel电子表格创建json。下面的代码生成一个dic,然后生成一个json,不过我想进一步修改代码,以便获得以下json。 我到目前为止运气不好 期望的结果: {“项目”:{ “AMS升级”:[ {“总计”:“30667”}, {“%Complete to end”:“100%”, {“索赔价值”:“25799”} ],“BMS工程”:[ {“总计”:“35722”}, {“%Complete to end”

我已经阅读了从csv创建嵌套JSON,但在我的案例中没有帮助

我想使用python从excel电子表格创建json。下面的代码生成一个dic,然后生成一个json,不过我想进一步修改代码,以便获得以下json。 我到目前为止运气不好

期望的结果

{“项目”:{ “AMS升级”:[ {“总计”:“30667”}, {“%Complete to end”:“100%”, {“索赔价值”:“25799”} ],“BMS工程”:[ {“总计”:“35722”}, {“%Complete to end”:“10%”, {“索赔价值”:“3572”} ]}}

当前结果

{“行项目”:{“AMS升级”:“30667”,“BMS修改”:“35722”}

当前代码:

book = xlrd.open_workbook("Example - supporting doc.xls")
first_sheet = book.sheet_by_index(-1)
nested_dict = {}


nested_dict["line items"] = {}
for i in range(21,175):
    Line_items = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=8)

    if str(Line_items[0].value) and str(Line_items[1].value):
        if not Line_items[5].value ==0 : 
            print str(Line_items[0].value)
            print str(Line_items[5].value)
            nested_dict["line items"].update({str(Line_items[0].value) : str(Line_items[1].value)})

print  nested_dict

print json.dumps(nested_dict)
Excel打印输出:

数据在哪里?对不起,我确实将代码修改为原始代码,忽略原始代码可能的重复
book = xlrd.open_workbook("Example - supporting doc.xls")
first_sheet = book.sheet_by_index(-1)
nested_dict = {}

nested_dict["line items"] = {}
col_names = {1: "Total", 2: "% Complete to end", 5: "value claimed"}

for i in range(21,175):
    Line_items = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=8)

    if str(Line_items[0].value) and str(Line_items[1].value):
        if not Line_items[5].value ==0 :
            inner_list = []
            for j in [1, 2, 5]:
                inner_list.append({col_names[j]: Line_items[j].value})
            nested_dict["line items"].update({str(Line_items[0].value) : inner_list})

print(nested_dict)

print(json.dumps(nested_dict))