简体   繁体   English

如何比较2个不同文件夹中2个同名的不同excel文件?

[英]How to compare 2 different excel files of same name in 2 different folder?

I have a set of excel sheet in one folder and another set in folder 2. If the same file name in both folders matches I need to take the difference in the cell. 我在一个文件夹中有一组excel工作表,在文件夹2中有另一组工作表。如果两个文件夹中的相同文件名都匹配,则需要在单元格中进行区别。

The difference in column I have a script below. 在列中的区别我下面有一个脚本。 How to pass for loop for this? 如何为此传递循环?

import pandas as pd
df1 = pd.read_excel('firstfolder/0.xls')
df2 = pd.read_excel('secondfolder/0.xls')
difference = df1[df1!=df2]
print (difference)

Assuming your folders contain the same xls files, and the file all have the same structure, then you can use glob and iterate - 假设您的文件夹包含相同的xls文件,并且文件都具有相同的结构,那么您可以使用glob并进行迭代-

import glob

diffs = []
for i, j in zip(*map(glob.glob, ['firstfolder/*.xls', 'secondfolder/*.xls'])):
    i, j = map(pd.read_excel, [i, j])
    diffs.append(i[i != j])

diff = pd.concat(diffs, axis=0)

courtesy : Compare 2 excel files using Python 礼貌: 使用Python比较2个excel文件

from itertools import zip_longest
import xlrd
import os

first_files = os.listdir('folder1')
second_files = os.listdir('folder2')
matches = [x for x in second_files if x in first_files]
#print(matches)

for (matches[0]) in matches:
    print (matches[0])
    rb1 = xlrd.open_workbook(os.path.join('folder1',matches[0]))
    rb2 = xlrd.open_workbook(os.path.join('folder2',matches[0]))
    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(zip_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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个不同文件夹中具有相同名称的两个.text文件的每一行? - How can I compare each line of two .text files with same name located in two different folder? 我在同一个文件夹中有不同的 excel 文件 - I have different excel files in the same folder 如何将 5 个同名但位于 5 个不同文件夹中的 Excel 文件合并? - How to combine 5 Excel files with the same name but in 5 different folders? 如何比较两个 Excel 文件中的不同值 - How to compare different values in two Excel files 如果同一文件夹中存在两个具有相同名称和不同结尾的文件,如何从文件夹中删除一个文件? - How to delete one file from a folder, if two files with the same name and different ending exist in the same folder? 比较两个文件中同名但值不同的两个 json 文件 - Compare two json files with same name but different values in two files 如何使用Python将同一文件夹中的不同Excel文件设置为不同版本? - How to set different excel files to different variales from the same folder using Python? 如何解压缩相同子目录但不同文件夹中的文件 - How to unzip files in the same subdirectories but a different folder 在不同位置显示相同文件夹名称的文件 - Display files from same folder name in different location 如何合并两个具有相同列名相同格式的 excel 文件,只有日期不同,并且列顺序应基于相同的列名 - How combine two excel files with the same columns name same format, only dates are different, and the column order should base on the same column name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM