python - 使用 PYTHON 计算多个 excel 文件中的行总和
问题描述
第 1 部分)我在文件夹路径中保存了多个 XLXS excel 文件:
文件名 行数 Excel 1 10 Excel 2 20 Excel 3 10 Excel 4 10 Excel 5 5
每个 XLXS 文件都包含相同的列,例如:姓名、邮政编码、地址、号码
每个都有不同的行数:
我需要一种快速的方法来对 PYTHON 上这 5 个 Excel 文件中的所有行进行计数和求和。(我是手动完成的,我从未使用过 Python,需要在 Python 上执行此操作)
期望的输出应该是:
总数= 55
第 2 部分) 现在我有了总计数,我需要比较记录的计数,以确保它们在另一个单独的 **** 文件(组合 5 个 xlxs 文件)中相同 -
它应该都匹配到 55 行
解决方案
如果文件扩展名为 .csv、.tsv,则此脚本应该执行您想要的操作,例如:
sm=0
for i in range(1,6):
lines = len(open("Excel {}".format(i)).readlines())
print("Lines for Excel {} = {}".format(i, lines))
sm = sm + lines
print("Sum of all lines = {}".format(sm))
编辑:由于它是 .xlsx 文件,因此您必须使用xlrd
库:
import xlrd
sm=0
for i in range(1,6):
lines = xlrd.open_workbook("Excel {}".format(i)).sheet_by_index(0).nrows
print("Lines for Excel {} = {}".format(i, lines))
sm = sm + lines
print("Sum of all lines = {}".format(sm))
推荐阅读
- python - 如何在 Python 中导入类
- flutter - 尝试在 Flutter 中的 MaterialPageRoute 之后显示对话时出现“'context != null': is not true”错误?
- python - pandas.DataFrame.last 给出整个数据帧而不是它的一部分(python)
- javascript - 有人可以解释一下这个例子中的功能流程吗?
- html - 如何让 div 占据屏幕的其余部分而不滚动?
- javascript - 将数字推送到 TypeScript 中的数组时出现问题
- swift - 在 Swift 的 NSDocument 应用程序中从行参数打开文件的正确方法是什么?
- google-cloud-platform - 是否可以同时创建多个谷歌云实例并将文件上传到所有实例并从中下载文件?
- python - BeautifulSoup:“发生异常:KeyError 'id'”使用带有 .find_all() 方法的函数时
- reactjs - 通过 map() 渲染多个组件时,componentDidMount 仅触发一次