老师,我这个是什么问题呀,代码检查了没有问题
代码:
import xlrd
from xlutils.copy import copy
def read_data():
wb = xlrd.open_workbook('./base_data/data01.xlsx')
sh = wb.sheet_by_index(0)
fen_type = {} # {a:110,b:300}
count_price = [] # [1,2,3,4,5]
for i in range(sh.nrows):
count = sh.cell_value(i,3) * sh.cell_value(i,4)
count_price.append(count)
key = sh.cell_value(i,0)
if fen_type.get(key):
fen_type[key] += count
else:
fen_type[key] = count
return fen_type,count_price # 各分类的总价值, 每个单品的总价值
def save(fen,count):
wb = xlrd.open_workbook('./base_data/data01.xlsx')
sh_t = wb.sheet_by_index(0)
wb2 = copy(wb)
sh = wb2.get_sheet(0)
for i in range(sh_t.nrows):
sh.write(i,sh_t.ncols,count[i])
sh2 = wb2.add_sheet('汇总的数据')
for x,key in enumerate(fen.keys()):
sh2.write(x,0,key)
sh2.write(x,1,fen.get(key))
wb2.save('./create_base/05_数据汇总.xlsx')
if __name__ == "__main__":
f,c = read_data()
save(f,c)
运行结果:
