[英]How to Create a Nested JSON using Python
我已经阅读了从csv创建嵌套JSON的内容,但对我而言没有帮助。
我想使用python从Excel电子表格创建一个json。 下面的代码生成一个dic然后是一个json,但是我想进一步修改代码,以便获得以下json。 到目前为止,我还没有运气。
预期结果 :
{“ items”:{“ AMS Upgrade”:[{“ Total”:“ 30667”},{“%Complete to end”:“ 100%”},{“ value value”:“ 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)
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.