使用Python比较2个excel文件

时间:2021-05-17 22:51:49

I have two xlsx files as follows:

我有两个xlsx文件如下:

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.

我需要比较所有单元格,如果来自file1的单元格!=来自file2的单元格打印那个单元格。

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 ?

如何添加file1和file2的比较单元?

2 个解决方案

#1


3  

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

#2


9  

Use pandas and you can do it as simple as this:

使用pandas,你可以这么简单:

import pandas as pd

df1 = pd.read_excel('excel1.xlsx')
df2 = pd.read_excel('excel2.xlsx')

difference = df1[df1!=df2]
print difference

And the result will look like this:

结果将如下所示:

使用Python比较2个excel文件

#1


3  

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

#2


9  

Use pandas and you can do it as simple as this:

使用pandas,你可以这么简单:

import pandas as pd

df1 = pd.read_excel('excel1.xlsx')
df2 = pd.read_excel('excel2.xlsx')

difference = df1[df1!=df2]
print difference

And the result will look like this:

结果将如下所示:

使用Python比较2个excel文件