最近在工作中,需要人工比對大量的excel格式報表,剛好剛學了Pyhon入門基礎知識,想著寫個東西練練手,不但能提高代碼編寫能力,還能減輕工作量,提高工作效率。說干就干,簡單的理了邏輯。首先,將目標表和源表的內容分別寫入到字典中,Excel表中不確定有沒有字段是唯一值,所以選擇了行號作為key值,一行的內容放到list中,然后從源表中取一行去目標表中遍歷。想好之后開始敲代碼了,在代碼編寫過程中遇到很多的問題,都是遇到一個查一個?;镜谋葘δ軐崿F(xiàn)后,就想著在加個日志記錄下比對結果。寫下此文記錄下,just do it.
下面是全部代碼
#-*- coding: utf-8 -*-
#比對兩個Excel文件內容的差異
#---------------------假設條件----------------
#1、源表和目標表格式一致
#2、不存在合并單元格
#3、第2行開始比對
#---------------------------------------------
import xlrd
import xlwt
import os
import time; # 引入time模塊
#往日志文件中追加內容函數(shù)
def writeappend_logfile(filename,content):
file=open(filename,'a') #以追加方式打開日志文件
time_now= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #系統(tǒng)時間格式化
file.writelines(time_now+':'+content+'\n') #寫入內容
file.close() #關閉文件
def read_excel(ori_path,tar_path,sub_name):#
success=0 #匹配一致數(shù)量
fail=0 #匹配不一致數(shù)量
origin_xls={} #存儲源xls文件
target_xls={} #比對的xls文件
wb_ori=xlrd.open_workbook(ori_path) #打開原始文件
wb_tar=xlrd.open_workbook(tar_path) #打開目標文件
sheet_num = len(wb_ori.sheets()) #源表子表數(shù)量
## for sheet_i in range(sheet_num): #excel中子頁面數(shù)量
## sheet_ori=wb_ori.sheet_by_index(sheet_i) #通過索引值獲取源表名
## sheet_tar=wb_tar.sheet_by_index(sheet_i) #通過索引值獲取源表名
startime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #獲取系統(tǒng)當前時間并格式化為格式
print (startime,' 開始比對...')
logname='log_'+startime[0:10]+'.log' #截取日期年月日構成日志文件名
logfile=open(logname,'w') #創(chuàng)建日志文件,如果文件存在則清空內容,不存在則創(chuàng)建,如果需要同時批量比對多張表,可以考慮將日志文件名作為參數(shù)傳入
logfile.writelines(startime+':【開始比對】...'+'\n') #寫入開始時間
logfile.close() #關閉日志文件
try:
sheet_ori=wb_ori.sheet_by_name(sub_name)
sheet_tar=wb_tar.sheet_by_name(sub_name)
if sheet_ori.name==sheet_tar.name:
#sheet表名
if sheet_ori.name==sub_name:
#先將數(shù)存入dictionary中dictionary(rows:list)
#第一行存儲表頭
#源表取一行數(shù)據(jù)與目標表全表進行比對如果表中存在主鍵可以用主鍵進行索引
#數(shù)據(jù)從excel第3行開始
for rows in range(1,sheet_ori.nrows):
orign_list=sheet_ori.row_values(rows) #源表i行數(shù)據(jù)
target_list=sheet_tar.row_values(rows) #目標表i行數(shù)據(jù)
origin_xls[rows]=orign_list #源表寫入字典
target_xls[rows]=target_list #目標表寫入字典
if origin_xls[1] == target_xls[1]:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 表頭一致')
for ori_num in origin_xls:
flag='false' #判斷是否一致標志
for tar_num in target_xls:
if origin_xls[ori_num]==target_xls[tar_num]:
flag='true'
break #如果匹配到結果退出循環(huán)
if flag=='true': #匹配上結果輸出后臺日志
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' row:%d is ok'%ori_num)
success+=1
else: #匹配不上將源表中行記錄寫入txt
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' row:%d is different'%ori_num)
fail+=1
data=origin_xls[ori_num]
logstr='【不一致】row<'+str(ori_num)+'>:'+str(data)
writeappend_logfile(logname,logstr)
# logstr='【比對完成】總記錄數(shù):'+str(ori_num)+'條,一致:'+str(success)+'條,不一致:'+str(fail)+'條'
logstr='【比對完成】總記錄數(shù):{:d}條,一致:{:d}條,不一致:{:d}條'.format(ori_num,success,fail)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 【%s】比對結束'%sheet_ori.name)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' 總記錄數(shù):%d條,一致:%d條,不一致:%d條'%(ori_num,success,fail))
writeappend_logfile(logname,logstr)
else:
errmsg='【'+sub_name+'】子表名不一致'
writeappend_logfile(logname,errmsg)
except Exception as err:
writeappend_logfile(logname,str(err)) #輸出異常
def main():
pass
if __name__ == '__main__':
read_excel(r'2.xls',1.xls','sheet1')
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文題目:Python3實現(xiàn)兩個Excel文件內容比對-創(chuàng)新互聯(lián)
網站網址:http://bm7419.com/article46/dicphg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供Google、外貿建站、App設計、建站公司、網站設計、ChatGPT
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容