7 python excel compare xlrd pandas
我有两个xlsx
文件如下:
value1 value2 value3
0.456 3.456 0.4325436
6.24654 0.235435 6.376546
4.26545 4.264543 7.2564523
Run Code Online (Sandbox Code Playgroud)
和
value1 value2 value3
0.456 3.456 0.4325436
6.24654 0.23546 6.376546
4.26545 4.264543 7.2564523
Run Code Online (Sandbox Code Playgroud)
我需要比较所有细胞,以及来自file1 !=
细胞的细胞file2
print
.
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
Run Code Online (Sandbox Code Playgroud)
如何添加的比较电池file1
和file2
?
Abb*_*bas 18
使用pandas
,你可以这么简单:
import pandas as pd
df1 = pd.read_excel('excel1.xlsx')
df2 = pd.read_excel('excel2.xlsx')
difference = df1[df1!=df2]
print difference
Run Code Online (Sandbox Code Playgroud)
结果将如下所示:
以下方法可以帮助您入门:
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)
Run Code Online (Sandbox Code Playgroud)
这将显示两个文件之间不同的任何单元格.对于您给定的两个文件,将显示:
Row 3 Col 2 - 0.235435 != 0.23546
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36202 次 |
最近记录: |