python对比excel文件,使用Python比较2个Excel文件

I have two xlsx files as follows:

value1 value2 value3

0.456 3.456 0.4325436

6.24654 0.235435 6.376546

4.26545 4.264543 7.2564523

and

value1 value2 value3

0.456 3.456 0.4325436

6.24654 0.23546 6.376546

4.26545 4.264543 7.2564523

I need to compare all cells, and if a cell from file1 != a cell from file2 print that.

import xlrd

rb = xlrd.open_workbook('file1.xlsx')

rb1 = xlrd.open_workbook('file2.xlsx')

sheet = rb.sheet_by_index(0)

for rownum in range(sheet.nrows):

row = sheet.row_values(rownum)

for c_el in row:

print c_el

How can I add the comparison cell of file1 and file2 ?

解决方案

The following approach should get you started:

from itertools import izip_longest

import xlrd

rb1 = 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)

This will display any cells which are different between the two files. For your given two files, this will display:

Row 3 Col 2 - 0.235435 != 0.23546

If you prefer cell names, then use xlrd.formular.colname():

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

Giving you:

Cell 3B 0.235435 != 0.23546

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值