1:打开文件,获取所需求平均值表格的内容
def write_excel(self):
"""将文本内容写入
sheet
data
"""
for sheet, data in self.data_dict.items():
index = 2
sheet_name = self.excel.create_sheet(sheet)
sheet_name['A1'] = '日期'
sheet_name['B1'] = '价格'
for row_data in data:
sheet_name['A'+str(index)] = row_data.split(',')[0]
sheet_name['B'+str(index)] = float(row_data.split(',')[1])
index += 1
sheet_name.cell(row=index, column=1).value = '平均价格'
sheet_name.cell(row=index, column=2).value = f"=AVERAGE(B2: B{index-1})"
上图所示:
求平均值 确保不是以 str类型保存
分析是 int 类型还是 float类型,进行数据强转
求取平均值 f"=AVERAGE(B2: B{index-1})"