l34Distinct

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/13 9:46
# @Author  : hu.zhezhong
# @Site    : 
# @File    : l34Distinct.py
# @Software: PyCharm Community Edition
from xlutils.copy import copy
import xlrd
import xml.etree.ElementTree as ET
import os

filterGrp_ruleDict = {}
def xlsProc():
    global ws, wb,table
    path = os.getcwd()
    files = os.listdir(path)
    for file in  files:
        if cmp(os.path.splitext(file)[len(os.path.splitext(file)) - 1], '.xls') == 0:
            xlsFile = file
            break
    try:
        rb = xlrd.open_workbook(xlsFile)
        wb = copy(rb)
    except:
        print ('there is no xls file, please run the related program to generate the xls file first!')
    ws = wb.get_sheet(0)
    table = rb.sheet_by_index(0)
    global filterGrpInd, ruleInd, rulebaseidInd
    filterGrpInd = 0
    ruleInd = 0
    rulebaseidInd = 0
    for i in range(table.ncols):
        if cmp(table.cell_value(0,i), 'filter-group') == 0:
            filterGrpInd = i
        elif cmp(table.cell_value(0,i), 'rule') == 0:
            ruleInd = i
        elif cmp(table.cell_value(0,i), 'rulebaseid') == 0:
            rulebaseidInd = i
    for row in range(1,table.nrows):
        filterGroup = table.cell_value(row, filterGrpInd)
        if cmp(filterGroup, '') != 0:              #l34层filtergroup
            rule = table.cell_value(row, ruleInd)
            rulebaseid = table.cell_value(row, rulebaseidInd)
            if not filterGrp_ruleDict.has_key(filterGroup):
                filterGrp_ruleDict[filterGroup] = []
                if {rule: rulebaseid} not in filterGrp_ruleDict[filterGroup]:
                    filterGrp_ruleDict[filterGroup].append({rule: rulebaseid})
            else:
                if {rule: rulebaseid} not in filterGrp_ruleDict[filterGroup]:
                    filterGrp_ruleDict[filterGroup].append({rule: rulebaseid})
    for value in filterGrp_ruleDict.values():
        (value0,) = value[0].values()
        for i in  range(1, len(value)):
            (rule,) = value[i].keys()
            for row in range(1,table.nrows):
                if cmp(table.cell_value(row, ruleInd), rule) == 0:
                    ws.write(row, rulebaseidInd, value0)
    wb.save(os.getcwd() + r'\ml-hwDPI-distinct.xls')

def xmlProc():
    path = os.getcwd()
    files = os.listdir(path)
    for file in files:
        if cmp(os.path.splitext(file)[len(os.path.splitext(file)) - 1], '.xml') == 0:
            xmlFile = file
            break
    try:
        global tree
        tree = ET.parse(xmlFile)
    except:
        print('there is no xml file, please run the related program to generate xml file first!')
    global root
    root = tree.getroot()

def distinct():
    template = root.find('template')
    for l4rule in template.findall('l4rule'):
        if l4rule.find('l7ruleset') == None:
            l4conditionset = l4rule.find('l4conditionset')
            for value in filterGrp_ruleDict.values():
                for i in range(1,len(value)):
                    (rule, )= value[i].keys()
                    if l4rule.get('name') == rule:
                        template.remove(l4rule)
    tree.write('malaxiya_distinct.xml')


# def distinct():
#     template = root.find('template')
#     for l4rule in template.findall('l4rule'):
#         if l4rule.find('l7ruleset') == None:
#             l4conditionset = l4rule.find('l4conditionset')
#             print l4rule.get('name')
#     #         template.remove(l4rule)
#     # tree.write('malaxiya_distinct.xml')
#


if __name__ == '__main__':
    xlsProc()
    xmlProc()
    distinct()

猜你喜欢

转载自blog.csdn.net/u014686859/article/details/87338204
34
l