pathon基礎2

支持不一樣文件,不一樣sheet頁
import xlrd
import xlwt
import os
import time;
#往日誌文件中追加內容函數
def writeLogfile(filename,content):
    file=open(filename,'a') #以追加方式打開日誌文件
    time_now= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #系統時間格式化
    file.writelines(time_now+':'+content+'\n')      #寫入內容
    file.close() #關閉文件
   
def read_excel(srcPath,tarPath,srcName,tarName):
    print(srcPath);
    print(tarPath);
    print(srcName);
    print(tarName);
    src_xls={}
    tar_xls={}
    src=xlrd.open_workbook(srcPath)
    tar=xlrd.open_workbook(tarPath)
    startime=time.strftime("%Y-%m-%d:%H:%M:%S",time.localtime())
    print(startime,'開始對比。。。')
    logName = 'log_'+startime[0:10]+'.log'
    logfile = open(logName,'w')
    logfile.writelines(startime+':[開始對比]...'+'\n')
    logfile.close()
    try:
        sheetSrc = src.sheet_by_name(srcName)
        sheetTar = tar.sheet_by_name(tarName)
        if sheetSrc.name == srcName:
            for row in range(0,sheetSrc.nrows):
                #獲取行內容行號列號從0開始
                #獲取列內容
                rowDesc=sheetSrc.row_values(row)
                #colDesc = sheetSrc.col_values(row)
                print(rowDesc)
                #print(colDesc)
                #獲取列內容
                colDesc=rowDesc[0]
                print(colDesc)
                #print(type(colDesc))
                colDesc2 = rowDesc[1]
                print(colDesc2)
                #print(type(colDesc))
                src_xls[colDesc]=colDesc2
               
        if sheetTar.name == tarName:
            for row in range(0,sheetTar.nrows):
                #獲取行內容行號列號從0開始
                #獲取列內容
                rowDesc=sheetTar.row_values(row)
                #colDesc = sheetSrc.col_values(row)
                print(rowDesc)
                #print(colDesc)
                #獲取列內容
                colDesc=rowDesc[0]
                print(colDesc)
                #print(type(colDesc))
                colDesc2 = rowDesc[1]
                print(colDesc2)
                #print(type(colDesc))
                tar_xls[colDesc]=colDesc2
               
        result = {}
        errcount = 0
        srccount = 0
        tarcount = 0
        for srcDes in list(src_xls.keys()):
            #if tar_xls.has_key(srcDes): pathon 已經刪除了has_key
            if srcDes not in tar_xls.keys():
                srccount+=1
                #logstr =' '+str(srcDes) +' '+str(src_xls[srcDes]) +' false';
                logstr=" ".join([str(srcDes),str(src_xls[srcDes]),false])
                writeLogfile(logName,str(logstr))
                result[errcount] = logstr
                errcount+=1
                continue
            for tarDes in list(tar_xls.keys()):
                if srcDes == tarDes and float(src_xls[tarDes]) == float(tar_xls[tarDes]):
                    #logstr =' '+str(srcDes) +' '+str(src_xls[srcDes]) + ' ' + str(tarDes) + ' ' + str(tar_xls[tarDes])+' true';
                    logstr = " ".join([str(srcDes),str(src_xls[srcDes]),str(tarDes),str(tar_xls[tarDes]),' true'])
                    writeLogfile(logName,logstr)
                    del src_xls[tarDes]
                    del tar_xls[tarDes]
                    continue
                    #result[errcount] = logstr
                    #errcount += 1
                elif srcDes == tarDes and float(src_xls[tarDes]) != float(tar_xls[tarDes]):
                   # logstr =' '+str(srcDes) +' '+str(src_xls[srcDes]) + ' ' + str(tarDes) + ' ' + str(tar_xls[tarDes])+ str(float(src_xls[tarDes])-float(tar_xls[tarDes]) )+ ' false';
                    logstr=" ".join([str(srcDes),str(src_xls[srcDes]),str(tarDes),str(tar_xls[tarDes]),str(float(src_xls[tarDes])-float(tar_xls[tarDes]) ), false])
                    writeLogfile(logName,str(logstr))
                    result[errcount] = logstr
                    errcount += 1
                    continue
        for tarDes in tar_xls:
                if(tarDes not in src_xls.keys()):
                    tarcount+=1
                    #logstr =' '+str(tarDes) +' '+str(tar_xls[tarDes]) +' false';
                    logstr = " ".join([str(tarDes),str(tar_xls[tarDes]),false])
                    writeLogfile(logName,str(logstr))
                    result[errcount] = logstr
                    errcount+=1
        writeLogfile(logName,'對比完成!!!錯誤數{:d}'.format(errcount))
        writeLogfile(logName,'對比完成!!!原數據文件獨有{:d}'.format(srccount))
        writeLogfile(logName,'對比完成!!!目標數據文件獨有{:d}'.format(tarcount))
        writeLogfile(logName,'差別數據:')
        for res in result:
            logstr =' '+str(res) +' '+result[res] ;
            writeLogfile(logName,str(logstr))
                   
    except Exception as err:
        print(str(err))
        writeLogfile(logName,str(err))
    def main():     pass if __name__ == '__main__':     read_excel('1007006.xlsx','1007006.xlsx','Sheet1','Sheet2')
相關文章
相關標籤/搜索