[英]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.