首页 > 编程知识 正文

用python对比两个Excel表数据,python对excel表格数据的统计和分析

时间:2023-05-04 01:10:38 阅读:285687 作者:2553

现在有两套结构一致的数据,放在80和2000两个文件夹中,文件名字相同,但文件数据不一致

80中的是xls格式存储,利用xlrd读取数据

2000中的是xlsx格式存储,利用openpyxl读取数据

然后对读取到的数据进行对比

第一种对比过后,将相应的子项内容罗列出来:

def compareData(fileName): file80 = get_filename_from_dir('F:\80') resultfx = [] if fileName in file80: wb80 = xlrd.open_workbook('80/'+fileName) wb2000 = xlrd.open_workbook('2000/'+fileName) sheet80_0 = wb80.sheet_by_index(0) sheet80_1 = wb80.sheet_by_index(1) sheet2000_0 = wb2000.sheet_by_index(0) sheet2000_1 = wb2000.sheet_by_index(1) test = sheet80_0.cell(0,1).value test2 = sheet2000_0.cell(5,0).value #表2 #print test,"+++++",test2 for i in range(3,8): for j in range(1,13): if(cmp(j,10)!=0): val80 = float(sheet80_0.cell(j,i).value) #print str(val80),"---",str(j),"---",str(i) if(cmp(j,8)==0): val2000 = float(sheet2000_0.cell(14,i).value) elif(cmp(j,6)==0 or cmp(j,7)==0): val2000 = float(sheet2000_0.cell(j+5,i).value) else: val2000 = float(sheet2000_0.cell(j+4,i).value) val_cha = val80 - val2000 if(cmp(val_cha,0.0000)!=0): L = [] lx = sheet80_0.cell(j,0).value+'---'+sheet80_0.cell(0,i).value L.append(lx) L.append(str(val80)) L.append(str(val2000)) resultfx.append(L) #表1 for m in range(1,20): val80_1 = float(sheet80_1.cell(1,m).value) val2000_1 = float(sheet2000_1.cell(5,m).value) val_cha1 = val80_1 - val2000_1 if(cmp(val_cha1,0.0000)!=0): L = [] lx = sheet80_1.cell(1,0).value+'---'+sheet80_1.cell(0,m).value L.append(lx) L.append(str(val80_1)) L.append(str(val2000_1)) resultfx.append(L) return resultfx else: print fileName +"-----无80数据"def myresult(fileName): wb = Workbook() index = 0 for d in data: sheet = wb.create_sheet('sheet'+str(index)) sheet.append([fileName,'80','2000']) sheet.append(d) index = index + 1 wb.save('对比结果.xlsx')

第二种是生成同样的表格结构,然后将差值填入:

r = get_filename_from_dir('F:\2000新')file80 = get_filename_from_dir('F:\80')wb = Workbook()index = 0for i in r: if i in file80: sheet = wb.create_sheet('sheet'+str(index)) sheet.append([i.decode('gbk'),'80'.decode('gbk'),'2000'.decode('gbk')]) dilei_head = [i,'图斑面积','图斑地类面积','线状地物、田坎面积','其中国有土地','其中集体土地'] gengdi_head = [i,'耕地','水田','水浇地','旱地','1等','2等','3等','4等','5等','6等','7等','8等','9等','10等','11等','12等','13等','14等','15等'] dilei_col = ['总面积','农用地','耕地','其中:水田','其中:水浇地','园地','林地','带K面积','其他(包含养殖)','建设用地','未利用地'] for t, item in enumerate(dilei_head): sheet.cell(row = 1,column=t+1,value=item.decode('gbk')) for t, item in enumerate(gengdi_head): sheet.cell(row = 14,column=t+1,value=item.decode('gbk')) for t, item in enumerate(dilei_col): sheet.cell(row = t+2,column=1,value=item.decode('gbk')) for t, item in enumerate(compareData(i)): if(t!=5): for m,mitem in enumerate(item): mitem = str(mitem).decode('gbk') sheet.cell(row = m+2,column=t+2,value=mitem) else: for m,mitem in enumerate(item): mitem = str(mitem).decode('gbk') sheet.cell(row = 15,column=m+2,value=mitem) index = index + 1 else: print i + "--无80数据"wb.save('对比结果kk.xlsx')

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。