Python3比對兩個表格


# get the list of files
'''
兩個比對的文件夾,極其路徑中名稱不允許出現數字!!!
'''
import os
import openpyxl
import re
f_ls_1=os.listdir(r'E:\test-ex') # init list of files
f_ls_2=os.listdir(r'E:\test-af') # next list of files
# compare two list
if (f_ls_1==f_ls_2)==False:
    print('文件列表不同')
    os.system('pause')
else:
    pass

for f in f_ls_1:
    domin=os.path.abspath(r'E:\test-ex')# remember the f shall and loop shall came two different list
    e_f=os.path.join(domin,f) #complete file path
    wb=openpyxl.load_workbook(e_f) 
    sheet=wb['Worksheet']
    dl_pick=re.compile
    dl_pick=re.compile(r'\d{1,9}')
    dlid=dl_pick.search(e_f)
    dl_id_s=str('dlid')+str(dlid.group())

    row_max=sheet.max_row
    col_max=sheet.max_column
    ls_1=[dl_id_s]
    for row_num in range(2,row_max+1):
        for col_num in range(1,col_max+1):
            ls_1.append(sheet.cell(row=row_num,column=col_num).value)


    domin=os.path.abspath(r'E:\test-af')# remember the f shall and loop shall came two different list
    e_f=os.path.join(domin,f)
    wb=openpyxl.load_workbook(e_f)
    sheet=wb['Worksheet']
    dl_pick=re.compile
    dl_pick=re.compile(r'\d{1,9}')
    dlid=dl_pick.search(e_f)
    dl_id_s=str('dlid')+str(dlid.group())

    row_max=sheet.max_row
    col_max=sheet.max_column
    ls_2=[dl_id_s]
    for row_num in range(2,row_max+1):
        for col_num in range(1,col_max+1):
            ls_2.append(sheet.cell(row=row_num,column=col_num).value)
        result=(ls_1==ls_2)
    if result==False:
        print(dl_id_s,result)
    pass

工作中要比較兩個文件夾中多個Excel表格是否相同


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM