【问题标题】:Compare 2 excel files using Python使用 Python 比较 2 个 excel 文件
【发布时间】:2016-09-03 22:26:30
【问题描述】:

我有两个xlsx 文件如下:

value1   value2   value3
0.456   3.456    0.4325436
6.24654 0.235435 6.376546
4.26545 4.264543 7.2564523

value1   value2  value3
0.456   3.456    0.4325436
6.24654 0.23546  6.376546
4.26545 4.264543 7.2564523

我需要比较所有单元格,如果一个单元格来自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

如何添加file1file2 的比较单元格?

【问题讨论】:

标签: python excel pandas compare xlrd


【解决方案1】:

使用pandas,您可以像这样简单地做到这一点:

import pandas as pd

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

difference = df1[df1!=df2]
print difference

结果将如下所示:

【讨论】:

    【解决方案2】:

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

    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)
    

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

    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
    

    【讨论】:

    • 如果要输出列名而不是列号,可以将 colnum+1 替换为 chr(colnum+65)
    • 获取列名的更好方法是使用xlrd.formula.colname(colnum)。然后,这将适用于超过 Z 列的工作表。
    【解决方案3】:

    我使用代码来做类似的事情。它有点笼统,效果很好。 Input Excel Sheets and expected Output Dataframe image

    import pandas as pd
    import numpy as np
    from xlsxwriter.utility import xl_rowcol_to_cell
    
    template = pd.read_excel("template.xlsx",na_values=np.nan,header=None)
    testSheet = pd.read_excel("test.xlsx",na_values=np.nan,header=None)
    
    rt,ct = template.shape
    rtest,ctest = testSheet.shape
    
    df = pd.DataFrame(columns=['Cell_Location','BaseTemplate_Value','CurrentFile_Value'])
    
    for rowNo in range(max(rt,rtest)):
      for colNo in range(max(ct,ctest)):
        # Fetching the template value at a cell
        try:
            template_val = template.iloc[rowNo,colNo]
        except:
            template_val = np.nan
    
        # Fetching the testsheet value at a cell
        try:
            testSheet_val = testSheet.iloc[rowNo,colNo]
        except:
            testSheet_val = np.nan
    
        # Comparing the values
        if (str(template_val)!=str(testSheet_val)):
            cell = xl_rowcol_to_cell(rowNo, colNo)
            dfTemp = pd.DataFrame([[cell,template_val,testSheet_val]],
                                  columns=['Cell_Location','BaseTemplate_Value','CurrentFile_Value'])
            df = df.append(dfTemp)
    

    df 是所需的数据帧

    【讨论】:

      【解决方案4】:
      df_file1 = pd.read_csv("Source_data.csv")
      df_file1 = df_file1.replace(np.nan, '', regex=True) #Replacing Nan with space
      df_file2 = pd.read_csv("Target_data.csv")
      df_file2 = df_file2.replace(np.nan, '', regex=True)
      
      df_i = pd.concat([df_file1, df_file2], axis='columns', keys=['file1', 'file2'])
      
      df_f = df_i.swaplevel(axis='columns')[df_s.columns[0:]]
      
      def highlight_diff(data, color='yellow'):
          attr = 'background-color: {}'.format(color)
          other = data.xs('file1', axis='columns', level=-1)
          return pd.DataFrame(np.where(data.ne(other, level=0), attr, ''), index=data.index, columns=data.columns)
      
      df_final = df_f.style.apply(highlight_diff, axis=None)
      
      writer = pd.ExcelWriter('comparexcels.xlsx')
      df_final.to_excel(writer)
       writer.save()
      

      Below Picture shows the output of file highlighting the differences

      【讨论】:

        【解决方案5】:
        import pandas as pd
        import numpy as np
        
        df1=pd.read_excel('Product_Category_Jan.xlsx')
        df2=pd.read_excel('Product_Category_Feb.xlsx')
        
        df1.equals(df2)
        
        comparison_values = df1.values == df2.values
        print (comparison_values)
        
        
        rows,cols=np.where(comparison_values==False)
        
        for item in zip(rows,cols):
            df1.iloc[item[0], item[1]] = '{} --> {}'.format(df1.iloc[item[0], 
            item[1]],df2.iloc[item[0], item[1]])
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-21
          • 2019-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-14
          • 1970-01-01
          相关资源
          最近更新 更多