1 Star 0 Fork 46

17010/qytang_Python

forked from cutecuteyu/qytang_Python 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
2016.02.19 试验台自动化 分析文件把信息写入字典 5.90 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-02-19 12:50 . new file
#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
import re
def filetodict(file):
Regex = []
R1_Regex = re.compile(r'======*R1======*')
Regex.append(R1_Regex)
R2_Regex = re.compile(r'======*R2======*')
Regex.append(R2_Regex)
R3_Regex = re.compile(r'======*R3======*')
Regex.append(R3_Regex)
R4_Regex = re.compile(r'======*R4======*')
Regex.append(R4_Regex)
R5_Regex = re.compile(r'======*R5======*')
Regex.append(R5_Regex)
R6_Regex = re.compile(r'======*R6======*')
Regex.append(R6_Regex)
ASA1_Regex = re.compile(r'======*ASA1======*')
Regex.append(ASA1_Regex)
ASA1SYS_Regex = re.compile(r'======*ASA1/SYS======*')
Regex.append(ASA1SYS_Regex)
ASA1ADMIN_Regex = re.compile(r'======*ASA1/admin======*')
Regex.append(ASA1ADMIN_Regex)
ASA1C1_Regex = re.compile(r'======*ASA1/c1======*')
Regex.append(ASA1C1_Regex)
ASA1C2_Regex = re.compile(r'======*ASA1/c2======*')
Regex.append(ASA1C2_Regex)
ASA2_Regex = re.compile(r'======*ASA2======*')
Regex.append(ASA2_Regex)
ASA2SYS_Regex = re.compile(r'======*ASA2/SYS======*')
Regex.append(ASA2SYS_Regex)
ASA2ADMIN_Regex = re.compile(r'======*ASA2/admin======*')
Regex.append(ASA2ADMIN_Regex)
ASA2C1_Regex = re.compile(r'======*ASA2/c1======*')
Regex.append(ASA2C1_Regex)
ASA2C2_Regex = re.compile(r'======*ASA2/c2======*')
Regex.append(ASA2C2_Regex)
ASA3_Regex = re.compile(r'======*ASA3======*')
Regex.append(ASA3_Regex)
ASA3SYS_Regex = re.compile(r'======*ASA3/SYS======*')
Regex.append(ASA3SYS_Regex)
ASA3ADMIN_Regex = re.compile(r'======*ASA3/admin======*')
Regex.append(ASA3ADMIN_Regex)
ASA3C1_Regex = re.compile(r'======*ASA3/c1======*')
Regex.append(ASA3C1_Regex)
ASA3C2_Regex = re.compile(r'======*ASA3/c2======*')
Regex.append(ASA3C2_Regex)
ASA4_Regex = re.compile(r'======*ASA4======*')
Regex.append(ASA4_Regex)
ASA4SYS_Regex = re.compile(r'======*ASA4/SYS======*')
Regex.append(ASA4SYS_Regex)
ASA4ADMIN_Regex = re.compile(r'======*ASA4/admin======*')
Regex.append(ASA4ADMIN_Regex)
ASA4C1_Regex = re.compile(r'======*ASA4/c1======*')
Regex.append(ASA4C1_Regex)
ASA4C2_Regex = re.compile(r'======*ASA4/c2======*')
Regex.append(ASA4C2_Regex)
SW1_Regex = re.compile(r'======*SW1======*')
Regex.append(SW1_Regex)
SW2_Regex = re.compile(r'======*SW2======*')
Regex.append(SW2_Regex)
SW3_Regex = re.compile(r'======*SW3======*')
Regex.append(SW3_Regex)
SW4_Regex = re.compile(r'======*SW4======*')
Regex.append(SW4_Regex)
SW5_Regex = re.compile(r'======*SW5======*')
Regex.append(SW5_Regex)
SW6_Regex = re.compile(r'======*SW6======*')
Regex.append(SW6_Regex)
IPS_Regex = re.compile(r'======*IPS======*')
Regex.append(IPS_Regex)
checkedfile = open(file, 'r')
checkedlines = checkedfile.readlines()
locations = []
for reg in Regex:
linelocation = 0
for line in checkedlines:
if reg.match(line):
locations.append(linelocation)
linelocation = linelocation + 1
length_of_location = len(locations)
# print(locations)
# print(length_of_location)
deviceconfigdict = {}
for configlocation in range(length_of_location):
# print(configlocation)
# print(length_of_location)
confignameline = checkedlines[locations[configlocation]:locations[configlocation]+1][0].strip()
# print(confignameline)
devicename = ''
for char in confignameline:
Device_Regex = re.compile(r'[a-zA-Z/0-9]')
if Device_Regex.match(char):
devicename = devicename + char
if configlocation < length_of_location - 1:
# print(configlocation)
# print(length_of_location)
# nowlocation = locations[configlocation]
# print(nowlocation)
# nextlocation = locations[configlocation+1]
# print(nextlocation)
deviceconfiglst = checkedlines[locations[configlocation]:locations[configlocation+1]]
else:
deviceconfiglst = checkedlines[locations[configlocation]:]
# print(devicename)
# print(type(deviceconfig))
# print(len(deviceconfig))
deviceconfig = ''
matched = 0
incorrect = re.compile(r'.*##.*')
for line in deviceconfiglst:
if incorrect.match(line):
matched = 1
deviceconfig = deviceconfig + line
# print(deviceconfiglst)
# deviceconfigbit = deviceconfig.encode()
# print(deviceconfigbit)
# print(devicename)
if matched == 1:
deviceconfigdict[devicename] = deviceconfig
# print(deviceconfigdict)
tmpfile = file.split('/')
filename = tmpfile[len(tmpfile) - 1]
values = filename.split('_')
deviceconfigdict['Rackno'] = values[0]
deviceconfigdict['verno'] = values[1]
deviceconfigdict['usrname'] = values[2]
deviceconfigdict['year'] = values[3]
deviceconfigdict['mon'] = values[4]
deviceconfigdict['day'] = values[5]
deviceconfigdict['hour'] = values[6]
deviceconfigdict['result'] = values[7][:2]
if values[7][:2] == 'A':
deviceconfigdict['score'] = 100
elif values[7][:2] == 'A-':
deviceconfigdict['score'] = 90
elif values[7][:2] == 'B+':
deviceconfigdict['score'] = 85
elif values[7][:2] == 'B':
deviceconfigdict['score'] = 80
elif values[7][:2] == 'B-':
deviceconfigdict['score'] = 75
elif values[7][:2] == 'C+':
deviceconfigdict['score'] = 70
elif values[7][:2] == 'C':
deviceconfigdict['score'] = 65
elif values[7][:2] == 'C-':
deviceconfigdict['score'] = 60
elif values[7][:2] == 'D+':
deviceconfigdict['score'] = 55
elif values[7][:2] == 'D':
deviceconfigdict['score'] = 50
elif values[7][:2] == 'D-':
deviceconfigdict['score'] = 45
elif values[7][:2] == 'E+':
deviceconfigdict['score'] = 40
elif values[7][:2] == 'E':
deviceconfigdict['score'] = 35
elif values[7][:2] == 'E-':
deviceconfigdict['score'] = 30
else:
deviceconfigdict['score'] = 80
return deviceconfigdict
if __name__ == '__main__':
dict = filetodict('/python/cgi-bin/checked/BJRack01_v10_sec-zzy_2016_2_19_12_A-.txt')
print(dict)
# print(dict.keys())
# for (device, config) in dict.items():
# print('*' * 30 + device + '*' * 30)
# print(config)
# print(dict['score'])
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lg_online/qytang_Python.git
git@gitee.com:lg_online/qytang_Python.git
lg_online
qytang_Python
qytang_Python
master

搜索帮助