使用python比较2个不同的excel文件

使用python比较2个不同的excel文件,python,Python,我有两个不同的excel文件,行数和列数不同。我必须根据唯一ID比较两个excel表中的金额,如果值有任何变化,则我必须获取这些结果并将行写入新的excel文件。此外,如果第二个excel中有任何新条目,则还需要将数据复制到新的excel中。两个文件中的行数不同。我尝试了以下方法,但它不起作用并返回TypeError:“Book”对象不可订阅if语句中的and条件,如果我只迭代行而不检查相同的索引,那么它将返回结果中缺少的行 from itertools import zip_longest i

我有两个不同的excel文件,行数和列数不同。我必须根据唯一ID比较两个excel表中的金额,如果值有任何变化,则我必须获取这些结果并将行写入新的excel文件。此外,如果第二个excel中有任何新条目,则还需要将数据复制到新的excel中。两个文件中的行数不同。我尝试了以下方法,但它不起作用并返回TypeError:“Book”对象不可订阅if语句中的and条件,如果我只迭代行而不检查相同的索引,那么它将返回结果中缺少的行

from itertools import zip_longest
import xlrd

rb1 = xlrd.open_workbook('./first_file1.xlsx')
rb2 = xlrd.open_workbook('./other_file1.xlsx')


sheet1 = rb1.sheet_by_index(0)
sheet2 = rb2``.sheet_by_index(0)

for rownum in range(max(sheet1.nrows, sheet2.nrows)):
    if (rownum < sheet2.nrows) and (rb1[0] == rb2[0]):
       row_rb1 = sheet1.row_values(rownum)
       row_rb2 = sheet2.row_values(rownum)

for colnum, (c1, c2) in enumerate(zip_longest(row_rb1, row_rb2)):
    if c1 != c2:
    print ("Cell {}{}  {} != {}".format(rownum+1, xlrd.formula.colname(colnum), c1, c2))
    else:
         print ("Row {} missing".format(rownum+1))
从itertools导入zip\u
导入xlrd
rb1=xlrd.open_工作簿(“./first_file1.xlsx”)
rb2=xlrd.open_工作簿('./其他_文件1.xlsx')
sheet1=rb1。按索引(0)排列的工作表
sheet2=rb2``。按索引(0)排列的工作表
对于范围内的行数(最大值(sheet1.nrows,sheet2.nrows)):
如果(rownum
您可以尝试以下方法:

rb1 = xlrd.open_workbook('./first_file1.xlsx')
rb2 = xlrd.open_workbook('./other_file1.xlsx')

sheet1 = rb1.sheet_by_index(0)
sheet2 = rb2.sheet_by_index(0)

new_df = []

for i, rownum_sheet2 in enumerate(range(sheet2.nrows)): #go through the (possible longer) sheet2
    row_rb2 = sheet2.row_values(rownum_sheet2)
    for rownum_sheet1 in range(sheet1.nrows): #go through sheet1 and check for same id
        row_rb1 = sheet1.row_values(rownum_sheet1)
        if row_rb1[0] == row_rb2[0]: #if the row with the same id is not equal: append to new df
            if row_rb1 != row_rb2:
                new_df.append(row_rb2)
    if i >= sheet1.nrows: #if there are extra rows, append to new df
        new_df.append(row_rb2)

#write new df to new excel-file
新代码:

df1_1 = pd.read_table('.../first_file.txt', sep = '/t')
df1_1.to_excel('filename1.xlsx') 
df_first_file = pd.concat([df1_1['Column'].str.split('  ',expand=True)],axis=1)
df_new1 = df_first_file.to_excel('first_file1.xlsx')


df1 = pd.read_table('.../otherfile.txt', sep = '/t')
df1.to_excel('filename2.xlsx') 
df_otherfile = pd.concat([df1['Column'].str.split('  ',expand=True)],axis=1)
df_new2 = df_otherfile.to_excel('other_file1.xlsx')

new_df = []

for i, rownum_sheet2 in enumerate(range(df_new2.nrows)): #go through the (possible longer) sheet2
    row_rb2 = df_new2.row_values(rownum_sheet2)
    for rownum_sheet1 in range(df_new1.nrows): #go through sheet1 and check for same id
        row_rb1 = df_new1.row_values(rownum_sheet1)
        if row_rb1[0] == row_rb2[0]: #if the row with the same id is not equal: append to new df
            if row_rb1 != row_rb2:
                new_df.append(row_rb2)
    if i >= df_new1.nrows: #if there are extra rows, append to new df
        new_df2 = new_df.append(row_rb2)
print (new_df2)
new_df.to_excel('final_filename.xlsx') 

我尝试过使用这种方法,但是它不是在excel工作表上迭代并返回:none因为您将它设置为excel工作表,所以您需要指定希望从该对象(->工作表)获得哪些数据。因此,您需要再次实现df_new1=df_new1.sheet_by_index(0)和df_new2=df_new2.sheet_by_index(0)。