我已经阅读了从csv创建嵌套的JSON,但对我来说没有帮助。
我想使用python从Excel电子表格创建一个json。下面的代码生成一个dic然后是一个json,但是我想进一步修改代码,以便获得以下json。 到目前为止,我还没有运气。
所需结果:
{“项目”:{ “ AMS升级”:[ {“总计”:“ 30667”}, {“%完成至结束”:“ 100%”}, {“要求的价值”:“ 25799”} ],“ BMS的工作原理”:[ {“总计”:“ 35722”}, {“%完成至结束”:“ 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)
答案 0 :(得分:1)
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))