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

yipeiwu_com5年前Python基础

如下所示:

# -*- 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')

以上这篇python进行两个表格对比的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python切片的步进、添加、连接简单操作示例

本文实例讲述了python切片的步进、添加、连接简单操作。分享给大家供大家参考,具体如下: 步进切片: #coding:utf-8 a="123456" print a[::-1]...

python处理html转义字符的方法详解

本文实例讲述了python处理html转义字符的方法。分享给大家供大家参考,具体如下: 最近在用Python处理网页数据时,经常遇到一些html转义字符(也叫html字符实体),例如&l...

python实现中文输出的两种方法

本文实例讲述了python实现中文输出的两种方法。分享给大家供大家参考。具体如下: 方法一: 用encode和decode 如: import os.path import xlrd...

Python批量查询关键词微信指数实例方法

Python批量查询关键词微信指数实例方法

教你用Python批量查询关键词微信指数。 前期准备安装好Python开发环境及Fiddler抓包工具。前期准备安装好Python开发环境及Fiddler抓包工具。 首先打开Fiddle...

举例讲解Python设计模式编程的代理模式与抽象工厂模式

代理模式 Proxy模式是一种常用的设计模式,它主要用来通过一个对象(比如B)给一个对象(比如A) 提供'代理'的方式方式访问。比如一个对象不方便直接引用,代理就在这个对象和访问者之间做...