Python全系列 教程
3567个小节阅读:5931k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
数据汇总是Excel中常用的功能,而在Python中使用通过遍历所有单元格即可实现
xxxxxxxxxx
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,6]
for r in range(sh.nrows):
count = sh.cell_value(r,3) * sh.cell_value(r,4)
count_price.append(count)
key = sh.cell_value(r,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 r in range(sh_t.nrows):
sh.write(r,sh_t.ncols,count[r])
sh2 = wb2.add_sheet('汇总的数据')
for i,key in enumerate(fen.keys()):
sh2.write(i,0,key)
sh2.write(i,1,fen.get(key))
wb2.save('./create_data/05_汇总数据.xlsx')
if __name__ == "__main__":
f,c = read_data()
save(f,c)
问题
xlrd.biffh.XLRDError: Excel xlsx file; not supported
解决方案:
更换版本
pip uninstall xlrd
pip install xlrd==1.2.0
实时效果反馈
1. 关于Python操作Excel,汇总数据说法错误的?
A 可以通过遍历所有单元格,再用Python汇总
B 可以使用Excel中函数汇总数据
C 可以直接使用Python的函数来汇总数据
答案
1=>C