pathon base 2

It supports different file, different sheet page
import xlrd
import xlwt
import os
import time;
# Additional content to the log file function
DEF writeLogfile (filename, Content):
    File = Open (filename, 'A') # open log file append mode
    time_now = time.strftime ( "% Y-% m-% d% H :% M:% S ", time.localtime ()) # system time format
    file.writelines (time_now + ':' + content + '\ n') # write to
    file.close () # close the file
   
def read_excel (srcPath , tarPath, SrcName, tarName):
    Print (by srcPath);
    Print (tarPath);
    Print (SrcName);
    Print (tarName);
    src_xls = {}
    tar_xls = {}
    the src = xlrd.open_workbook (by srcPath)
    the tar xlrd.open_workbook = ( tarPath)
    The time.strftime = Startime ( "% D% Y-M-%:% H:% M:% S", time.localtime ())
    Print (Startime, 'start comparison ...')
    logName = 'log _' + '. log' Startime [0:10] +
    logfile = Open (logName, 'W')
    logfile.writelines (Startime + ': [start Comparative] ...' + '\ n-')
    logfile.close ()
    the try:
        sheetSrc src.sheet_by_name = (SrcName)
        sheetTar = tar.sheet_by_name (tarName)
        IF sheetSrc.name == SrcName:
            for row in the Range (0, sheetSrc.nrows):
                # Gets the line line number out content from 0
                # to obtain column content
                = sheetSrc.row_values rowDesc (Row)
                #colDesc = sheetSrc. col_values(row)
                print(rowDesc)
                #Print (colDesc)
                # column contents acquired
                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 the Range (0, sheetTar.nrows):
                # Gets the line line number out content from 0
                # to obtain column content
                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 src_xls:
            #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';
                writeLogfile(logName,str(logstr))
                result[errcount] = logstr
                errcount+=1
               
            for tarDes in tar_xls:
                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';
                    writeLogfile(logName,logstr)
                    #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';
                    writeLogfile(logName,str(logstr))
                    result[errcount] = logstr
                    errcount += 1
                   
        for tarDes in tar_xls:
                if(tarDes not in src_xls.keys()):
                    tarcount+=1
                    logstr =' '+str(tarDes) +' '+str(tar_xls[tarDes]) +' false';
                    writeLogfile(logName,str(logstr))
                    result[errcount] = logstr
                    errcount+=1
        writeLogfile (logName, 'number of errors in comparison to complete! {:} D'. the format (errcount))
        writeLogfile (logName, 'Comparative complete original data file unique! {:} D'. the format (srccount))
        writeLogfile (logName, 'complete contrast target data file unique! {:} D' the format (tarcount).)
        writeLogfile (logName, 'difference data:')
        for RES in Result:
            logstr = '' + STR (RES) + '' + Result [RES];
            writeLogfile (logName, STR (logstr))
                   
    the 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')

Guess you like

Origin www.cnblogs.com/CoderRdf/p/11427023.html