python进行两个表格对比的方法

作者:黑面狐 时间:2021-12-15 20:31:57 

如下所示:


# -*- coding:utf-8 -*-
import xlrd
import sys
import re
import json

dict1={}
dict2={}
mylist=[u'系统运维管理',u'安全管理机构',u'安全管理制度',u'人员安全管理',u'网络安全',u'物理安全',u'网络安全',u'主机安全',u'应用安全',u"网络安全",u"主机安全",u"主机安全",u'系统建设管理']
def check():
 data=xlrd.open_workbook('test.xls')
 for i in range(0,13):
   print u"第%s个表格"%(i+1)
   print mylist[i]
   print "-"*60
   try:
     table=data.sheets()[i]
   except IndexError,e:
     break  
   nrows=table.nrows
   count=1
   if i<5:
     m=10
   else:
     m=16
   for j in range(m,nrows):
     cell_1=table.cell(j,2).value
     #print cell_1,
     cell_2=table.cell(j,3).value
     nu=re.split(r'\)',cell_2)
     num=re.split(r'\)',nu[0])
     if num is not None:
       #print num[0],
       pass
     else:
       print u"没有标记序号"
     try:
       if j<nrows:
         if table.cell(j,2).value==table.cell(j+1,2).value:
           count+=1
         else:
           print u"检查项【%s】共有【%s】项"%(table.cell(j,2).value,count)
           key=mylist[i]+table.cell(j,2).value
           dict1[key]=count
           count=1
       else:
         count+=1
         print u"检查项【%s】共有【%s】项"%(table.cell(j,2).value,count)
         key=mylist[i]+table.cell(j,2).value
         dict1[key]=count

except IndexError,e:
       #count+=1
       print u"检查项【%s】共有【%s】项"%(table.cell(j,2).value,count)
       key=mylist[i]+table.cell(j,2).value
       dict1[key]=count
   print "-"*60
 print json.dumps(dict1, encoding='UTF-8', ensure_ascii=False)
 print u"完成excel表格读取-Thanks"

def ASGcheck(filename):
 data=xlrd.open_workbook(filename)
 table=data.sheets()[0]
 print "-"*60
 nrows=table.nrows
 count=1
 for j in range(1,nrows):
   cell_1=table.cell(j,2).value
   #print cell_1,
   cell_2=table.cell(j,3).value
   nu=re.split(r'\)',cell_2)
   num=re.split(r'\)',nu[0])
   if num is not None:
     #print num[0],
     pass
   else:
     print u"没有标记序号"
   try:
     if j<nrows:
       if table.cell(j,3).value==table.cell(j+1,3).value:
         count+=1
       else:
         print u"安全层面【%s】检查项【%s】共有【%s】项"%(table.cell(j,2).value,table.cell(j,3).value,count)
         key=table.cell(j,2).value+table.cell(j,3).value
         dict2[key]=count
         count=1
     else:
       count+=1
       print u"安全层面【%s】检查项【%s】共有【%s】项"%(table.cell(j,2).value,table.cell(j,3).value,count)
       key=table.cell(j,2).value+table.cell(j,3).value
       dict2[key]=count

except IndexError,e:
     #count+=1
     print u"安全层面【%s】检查项【%s】共有【%s】项"%(table.cell(j,2).value,table.cell(j,3).value,count)
     key=table.cell(j,2).value+table.cell(j,3).value
     dict2[key]=count
 #print json.dumps(dict2, encoding='UTF-8', ensure_ascii=False)
 print "-"*60
def standard():
 choiceA=raw_input(u"请输入A的等级:A2,A3,A4")
 choiceS=raw_input(u"请输入S的等级:S2,S3,S4")
 choiceG=raw_input(u"请输入G的等级:G2,G3,G4")
 Aname=str(choiceA)+".xlsx"
 Sname=str(choiceS)+".xlsx"
 Gname=str(choiceG)+".xlsx"
 check()

def compare():
   print "*"*60
   for key in dict2:
     try:
       if dict2[key]!=dict1[key]:
         print u"存在异常项"
         print u"从程序中导出的检查项【%s】共有%s项"%(key,json.dumps(dict1[key], encoding='UTF-8', ensure_ascii=False))
         print u"从分支查询的检查项【%s】共有%s项"%(key,json.dumps(dict2[key], encoding='UTF-8', ensure_ascii=False))
       else:
         pass

except KeyError,e:
       print u"分支中的检查项【%s】共有%s项"%(key,json.dumps(dict2[key], encoding='UTF-8', ensure_ascii=False)),
       print u"程序中没有查询到该项"

#比较分支结果和程序导出的结果  
 ASGcheck(Aname)
 compare()
 ASGcheck(Sname)
 compare()
 ASGcheck(Gname)
 compare()
standard()
#ASGcheck('S3.xlsx')

来源:https://blog.csdn.net/qq1124794084/article/details/52863231

标签:python,表格
0
投稿

猜你喜欢

  • 使用Django启动命令行及执行脚本的方法

    2022-03-08 22:10:16
  • Python采集C站热榜数据实战示例

    2022-05-03 13:13:13
  • html风格tooltip效果的实现

    2010-04-08 13:00:00
  • 详解Python中pyautogui库的最全使用方法

    2022-11-17 17:07:14
  • 对跨多个表格的数据组合时需要用到的SQL

    2009-01-06 11:18:00
  • python脚本框架webpy入门安装及应用创建

    2023-08-23 05:08:51
  • SQL Server 2000 SP4补丁打不上的解决办法

    2010-03-08 13:13:00
  • Javascript实现的鼠标经过时播放声音

    2010-05-18 20:03:00
  • SQL 字母数字混合型字段 按里面的数字排序

    2010-04-23 18:18:00
  • 3段Python图像处理的实用代码的分享

    2021-10-19 08:06:58
  • PHP的mysqli_rollback()函数讲解

    2023-06-12 08:58:03
  • Python NumPy教程之二元计算详解

    2023-12-16 18:25:04
  • python将二维数组升为一维数组或二维降为一维方法实例

    2023-07-25 07:51:59
  • php filter协议使用方法

    2023-05-29 19:36:40
  • php小技巧之过滤ascii控制字符

    2023-10-03 05:13:15
  • 表单验证中时间起止判断的递归处理

    2009-12-16 19:27:00
  • Python函数参数基础介绍及示例

    2021-02-03 03:11:04
  • asp中文URL编码server.urlencode

    2008-08-08 11:22:00
  • 探索Python数据可视化库中Plotly Express的使用方法

    2022-10-29 13:45:31
  • 如何快速定位页面中复杂 CSS BUG 问题

    2009-01-15 12:23:00
  • asp之家 网络编程 m.aspxhome.com