当前位置:   article > 正文

日常使用————python对比两个excel表中的数据_python 遍历两个表 结果对比

python 遍历两个表 结果对比

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

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

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

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

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

  1. def compareData(fileName):
  2. file80 = get_filename_from_dir('F:\\80')
  3. resultfx = []
  4. if fileName in file80:
  5. wb80 = xlrd.open_workbook('80/'+fileName)
  6. wb2000 = xlrd.open_workbook('2000/'+fileName)
  7. sheet80_0 = wb80.sheet_by_index(0)
  8. sheet80_1 = wb80.sheet_by_index(1)
  9. sheet2000_0 = wb2000.sheet_by_index(0)
  10. sheet2000_1 = wb2000.sheet_by_index(1)
  11. test = sheet80_0.cell(0,1).value
  12. test2 = sheet2000_0.cell(5,0).value
  13. #表2
  14. #print test,"+++++",test2
  15. for i in range(3,8):
  16. for j in range(1,13):
  17. if(cmp(j,10)!=0):
  18. val80 = float(sheet80_0.cell(j,i).value)
  19. #print str(val80),"---",str(j),"---",str(i)
  20. if(cmp(j,8)==0):
  21. val2000 = float(sheet2000_0.cell(14,i).value)
  22. elif(cmp(j,6)==0 or cmp(j,7)==0):
  23. val2000 = float(sheet2000_0.cell(j+5,i).value)
  24. else:
  25. val2000 = float(sheet2000_0.cell(j+4,i).value)
  26. val_cha = val80 - val2000
  27. if(cmp(val_cha,0.0000)!=0):
  28. L = []
  29. lx = sheet80_0.cell(j,0).value+'---'+sheet80_0.cell(0,i).value
  30. L.append(lx)
  31. L.append(str(val80))
  32. L.append(str(val2000))
  33. resultfx.append(L)
  34. #表1
  35. for m in range(1,20):
  36. val80_1 = float(sheet80_1.cell(1,m).value)
  37. val2000_1 = float(sheet2000_1.cell(5,m).value)
  38. val_cha1 = val80_1 - val2000_1
  39. if(cmp(val_cha1,0.0000)!=0):
  40. L = []
  41. lx = sheet80_1.cell(1,0).value+'---'+sheet80_1.cell(0,m).value
  42. L.append(lx)
  43. L.append(str(val80_1))
  44. L.append(str(val2000_1))
  45. resultfx.append(L)
  46. return resultfx
  47. else:
  48. print fileName +"-----无80数据"
  49. def myresult(fileName):
  50. wb = Workbook()
  51. index = 0
  52. for d in data:
  53. sheet = wb.create_sheet('sheet'+str(index))
  54. sheet.append([fileName,'80','2000'])
  55. sheet.append(d)
  56. index = index + 1
  57. wb.save('对比结果.xlsx')

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

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

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/444211
推荐阅读
相关标签
  

闽ICP备14008679号