栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python比较2个Excel文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python比较2个Excel文件

以下方法应该可以帮助您入门:

from itertools import izip_longestimport xlrdrb1 = xlrd.open_workbook('file1.xlsx')rb2 = xlrd.open_workbook('file2.xlsx')sheet1 = rb1.sheet_by_index(0)sheet2 = rb2.sheet_by_index(0)for rownum in range(max(sheet1.nrows, sheet2.nrows)):    if rownum < sheet1.nrows:        row_rb1 = sheet1.row_values(rownum)        row_rb2 = sheet2.row_values(rownum)        for colnum, (c1, c2) in enumerate(izip_longest(row_rb1, row_rb2)): if c1 != c2:     print "Row {} Col {} - {} != {}".format(rownum+1, colnum+1, c1, c2)    else:        print "Row {} missing".format(rownum+1)

这将显示两个文件之间不同的所有单元格。对于给定的两个文件,将显示:

Row 3 Col 2 - 0.235435 != 0.23546

如果您更喜欢单元格名称,请使用

xlrd.formular.colname()

print "Cell {}{}  {} != {}".format(rownum+1, xlrd.formula.colname(colnum), c1, c2)

给你:

Cell 3B  0.235435 != 0.23546


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/651819.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号