其他分享
首页 > 其他分享> > pathon基础2

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 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,'对比完成!!!错误数{: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')

标签:pathon,tar,基础,logstr,str,print,tarDes,xls
来源: https://www.cnblogs.com/CoderRdf/p/11427023.html