这是我编写的代码。 将JSON数据输入EXEL。
我尝试了.format()但失败了。
我认为效率太低。 我应该怎么做才能使其变得简单?
寻求帮助
数据:
{"Y": ["1950","1951","1952","1953","1954","1955"],
"DATA":[
{"M":"1월",
"DATA1":164, "DATA2":173, "DATA3":2374,
"DATA4":183, "DATA5":186, "DATA6":2374,
"DATA7":151, "DATA8":145
},
{"M":"2월",
"DATA1":174, "DATA2":138, "DATA3":2374,
"DATA4":178, "DATA5":104, "DATA6":2374,
"DATA7":178, "DATA8":142
},
{"M":"3월",
"DATA1":135,"DATA2":173,"DATA3":2374,
"DATA4":104,"DATA5":176,"DATA6":2374,
"DATA7":134,"DATA8":178
},
{"M":"4월",
"DATA1":178,"DATA2":102,"DATA3":2374,
"DATA4":175,"DATA5":142,"DATA6":2374,
"DATA7":101,"DATA8":112
},
{"M":"5월",
"DATA1":135,"DATA2":168,"DATA3":2374,
"DATA4":127,"DATA5":104,"DATA6":2374,
"DATA7":167,"DATA8":183
},
{"M":"6월",
"DATA1":102,"DATA2":175,"DATA3":2374,
"DATA4":178,"DATA5":190,"DATA6":2374,
"DATA7":197,"DATA8":180},
{"M":"7월",
"DATA1":135,"DATA2":105,"DATA3":2374,
"DATA4":168,"DATA5":145,"DATA6":2374,
"DATA7":199,"DATA8":140},
{"M":"8월",
"DATA1":178,"DATA2":137,"DATA3":2374,
"DATA4":100,"DATA5":168,"DATA6":2374,
"DATA7":142,"DATA8":199
},
{"M":"9월",
"DATA1":135,"DATA2":175,"DATA3":2374,
"DATA4":135,"DATA5":124,"DATA6":2374,
"DATA7":176,"DATA8":125
},
{"M":"10월",
"DATA1":140,"DATA2":105,"DATA3":2374,
"DATA4":133,"DATA5":133,"DATA6":2374,
"DATA7":104,"DATA8":104
},
{"M":"11월",
"DATA1":199,"DATA2":178,"DATA3":2374,
"DATA4":170,"DATA5":157,"DATA6":2374,
"DATA7":178,"DATA8":167
},
{"M":"12월",
"DATA1":138,"DATA2":196,"DATA3":2374,
"DATA4":109,"DATA5":107,"DATA6":2374,
"DATA7":100,"DATA8":104
}
],
"단위":"건","오차":"5%"
}
代码:
import json
from openpyxl import Workbook
wb = Workbook()
wb.save('test1.xlsx')
sheet1 = wb.active
sheet1.title = 'abcd'
with open('getdata.JSON', encoding='UTF8') as resp:
project_info = json.load(resp)
sheet1['B10'] = project_info['Y'][0]
sheet1['C10'] = project_info['Y'][1]
sheet1['D10'] = project_info['Y'][2]
sheet1['E10'] = project_info['Y'][3]
sheet1['F10'] = project_info['Y'][4]
sheet1['G10'] = project_info['Y'][5]
sheet1['A11'] = project_info['DATA'][0].get('M')
sheet1['A12'] = project_info['DATA'][1].get('M')
sheet1['A13'] = project_info['DATA'][2].get('M')
sheet1['A14'] = project_info['DATA'][3].get('M')
sheet1['A15'] = project_info['DATA'][4].get('M')
sheet1['A16'] = project_info['DATA'][5].get('M')
sheet1['A17'] = project_info['DATA'][6].get('M')
sheet1['A18'] = project_info['DATA'][7].get('M')
sheet1['A19'] = project_info['DATA'][8].get('M')
sheet1['A20'] = project_info['DATA'][9].get('M')
sheet1['A21'] = project_info['DATA'][10].get('M')
sheet1['A22'] = project_info['DATA'][11].get('M')
sheet1['B11'] = project_info['DATA'][0].get('DATA1')
sheet1['B12'] = project_info['DATA'][1].get('DATA1')
sheet1['B13'] = project_info['DATA'][2].get('DATA1')
sheet1['B14'] = project_info['DATA'][3].get('DATA1')
sheet1['B15'] = project_info['DATA'][4].get('DATA1')
sheet1['B16'] = project_info['DATA'][5].get('DATA1')
sheet1['B17'] = project_info['DATA'][6].get('DATA1')
sheet1['B18'] = project_info['DATA'][7].get('DATA1')
sheet1['B19'] = project_info['DATA'][8].get('DATA1')
sheet1['B20'] = project_info['DATA'][9].get('DATA1')
sheet1['B21'] = project_info['DATA'][10].get('DATA1')
sheet1['B22'] = project_info['DATA'][11].get('DATA1')
sheet1['C11'] = project_info['DATA'][0].get('DATA2')
sheet1['C12'] = project_info['DATA'][1].get('DATA2')
sheet1['C13'] = project_info['DATA'][2].get('DATA2')
sheet1['C14'] = project_info['DATA'][3].get('DATA2')
sheet1['C15'] = project_info['DATA'][4].get('DATA2')
sheet1['C16'] = project_info['DATA'][5].get('DATA2')
sheet1['C17'] = project_info['DATA'][6].get('DATA2')
sheet1['C18'] = project_info['DATA'][7].get('DATA2')
sheet1['C19'] = project_info['DATA'][8].get('DATA2')
sheet1['C20'] = project_info['DATA'][9].get('DATA2')
sheet1['C21'] = project_info['DATA'][10].get('DATA2')
sheet1['C22'] = project_info['DATA'][11].get('DATA2')
sheet1['D11'] = project_info['DATA'][0].get('DATA4')
sheet1['D12'] = project_info['DATA'][1].get('DATA4')
sheet1['D13'] = project_info['DATA'][2].get('DATA4')
sheet1['D14'] = project_info['DATA'][3].get('DATA4')
sheet1['D15'] = project_info['DATA'][4].get('DATA4')
sheet1['D16'] = project_info['DATA'][5].get('DATA4')
sheet1['D17'] = project_info['DATA'][6].get('DATA4')
sheet1['D18'] = project_info['DATA'][7].get('DATA4')
sheet1['D19'] = project_info['DATA'][8].get('DATA4')
sheet1['D20'] = project_info['DATA'][9].get('DATA4')
sheet1['D21'] = project_info['DATA'][10].get('DATA4')
sheet1['D22'] = project_info['DATA'][11].get('DATA4')
sheet1['E11'] = project_info['DATA'][0].get('DATA5')
sheet1['E12'] = project_info['DATA'][1].get('DATA5')
sheet1['E13'] = project_info['DATA'][2].get('DATA5')
sheet1['E14'] = project_info['DATA'][3].get('DATA5')
sheet1['E15'] = project_info['DATA'][4].get('DATA5')
sheet1['E16'] = project_info['DATA'][5].get('DATA5')
sheet1['E17'] = project_info['DATA'][6].get('DATA5')
sheet1['E18'] = project_info['DATA'][7].get('DATA5')
sheet1['E19'] = project_info['DATA'][8].get('DATA5')
sheet1['E20'] = project_info['DATA'][9].get('DATA5')
sheet1['E21'] = project_info['DATA'][10].get('DATA5')
sheet1['E22'] = project_info['DATA'][11].get('DATA5')
sheet1['F11'] = project_info['DATA'][0].get('DATA7')
sheet1['F12'] = project_info['DATA'][1].get('DATA7')
sheet1['F13'] = project_info['DATA'][2].get('DATA7')
sheet1['F14'] = project_info['DATA'][3].get('DATA7')
sheet1['F15'] = project_info['DATA'][4].get('DATA7')
sheet1['F16'] = project_info['DATA'][5].get('DATA7')
sheet1['F17'] = project_info['DATA'][6].get('DATA7')
sheet1['F18'] = project_info['DATA'][7].get('DATA7')
sheet1['F19'] = project_info['DATA'][8].get('DATA7')
sheet1['F20'] = project_info['DATA'][9].get('DATA7')
sheet1['F21'] = project_info['DATA'][10].get('DATA7')
sheet1['F22'] = project_info['DATA'][11].get('DATA7')
sheet1['G11'] = project_info['DATA'][0].get('DATA8')
sheet1['G12'] = project_info['DATA'][1].get('DATA8')
sheet1['G13'] = project_info['DATA'][2].get('DATA8')
sheet1['G14'] = project_info['DATA'][3].get('DATA8')
sheet1['G15'] = project_info['DATA'][4].get('DATA8')
sheet1['G16'] = project_info['DATA'][5].get('DATA8')
sheet1['G17'] = project_info['DATA'][6].get('DATA8')
sheet1['G18'] = project_info['DATA'][7].get('DATA8')
sheet1['G19'] = project_info['DATA'][8].get('DATA8')
sheet1['G20'] = project_info['DATA'][9].get('DATA8')
sheet1['G21'] = project_info['DATA'][10].get('DATA8')
sheet1['G22'] = project_info['DATA'][11].get('DATA8')
wb.save('test1.xlsx')
答案 0 :(得分:1)
您应该能够在字母上使用嵌套循环
这将使字母b到g循环(字母的索引1到7,根据需要进行调整),然后每个字母创建12个条目
import string
for idx, letter in enumerate(string.ascii_lowercase[1:7]):
for i in range(12):
sheet1[f'{letter}{i+11}'] = project_info['DATA'][i].get(f'DATA{idx + 1}')
(写在电话上,为格式化表示歉意)
您可以对a列和y信息使用类似的循环
答案 1 :(得分:1)
sheet = ['B10', 'C10', 'D10', 'E10', 'F10', 'G10']
for i in range(0,5,1):
sheet1[sheet[i]] = project_info['Y'][i]
_dict = {"A":"M", "B":"DATA1", "C":"DATA2", "D":"DATA4", "E":"DATA5", "F":"DATA7", "G":"DATA8" }
for k, v in _dict.items():
for i in range(0, 11, 1):
key = k + str(i+11)
sheet1[key] = project_info['DATA'][i].get(v)