我有一个自定义主机文件,如下所示:
[test_components:children]
test1
test2
test3
[test1]
LOTOTES[01:44]
[test2]
DRIVETES[49:54]
[test3]
SIMTES[1:2]
现在解析文件,我的python代码如下:
import os
import ast
import argparse
import re
import sys
import glob
#Command line Args used
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--filename", required=True, help="Desired host file")
args = parser.parse_args()
filename = args.filename
def getgrandchild(child):
nodelist = []
if child is None:
return
for nodes in child:
if re.match(r".*(\[[0-9]+\:[0-9]+\])", nodes):
m = re.search(r"(\w+)\[([[0-9]+)\:([0-9]+)\]", nodes)
lb = int(m.group(2))
ub = int(m.group(3))
for i in range(lb, ub+1):
nodelist.append(m.group(1)+str(i))
elif (r".*(\[[0]+\[0-9]+\:[0-9]+\])", nodes):
m = re.search(r"(\w+)\[([([0])\[0-9]+)\:([0-9]+)\]", nodes)
lb = int(m.group(2))
ub = int(m.group(3))
for i in range(lb, ub+1):
nodelist.append(m.group(1)+str(i))
nodelist.append(m.group(1))
elif re.match(r"(\w+)", nodes):
m = re.search(r"(\w+)", nodes)
nodelist.append(m.group(1))
return(nodelist)
def getnewchild(path, group_list):
entities = []
childgroup_flag = 0
for teststring in group_list:
childteststring = teststring+":children"
with open(path) as f:
in_flag = 0
break_flag = 0
for line in f:
line=line.rstrip()
if re.match(r"^\[(.*)\]", line) and in_flag == 0:
if in_flag == 1 and break_flag == 1:
return(entities)
m = re.search(r"\[(.*)\]", line)
if teststring == m.group(1) or childteststring == m.group(1):
in_flag = 1
elif re.match(r"\[(.*)\]", line) and in_flag == 1:
return(entities)
elif in_flag == 1:
new_group_list = [line]
new_entities = getnewchild(path, new_group_list)
if new_entities is None:
entities.append(line)
else:
entities.extend(new_entities)
if __name__== "__main__":
FILE_PATH = "{filename}".format(filename=filename)
GROUP_HEADER = "test1"
group_list = [GROUP_HEADER]
new_group_list = getnewchild(FILE_PATH,group_list)
testnodelist = getgrandchild(new_group_list)
print(testnodelist)
现在,当我执行时,我得到以下输出:
['LOTOTES1', 'LOTOTES2', 'LOTOTES3', 'LOTOTES4', 'LOTOTES5', 'LOTOTES6', 'LOTOTES7', 'LOTOTES8', 'LOTOTES9', 'LOTOTES10', 'LOTOTES11', 'LOTOTES12', 'LOTOTES13', 'LOTOTES14', 'LOTOTES15', 'LOTOTES16', 'LOTOTES17', 'LOTOTES18', 'LOTOTES19', 'LOTOTES20', 'LOTOTES21', 'LOTOTES22', 'LOTOTES23', 'LOTOTES24', 'LOTOTES25', 'LOTOTES26', 'LOTOTES27', 'LOTOTES28', 'LOTOTES29', 'LOTOTES30', 'LOTOTES31', 'LOTOTES32', 'LOTOTES33', 'LOTOTES34', 'LOTOTES35', 'LOTOTES36', 'LOTOTES37', 'LOTOTES38', 'LOTOTES39', 'LOTOTES40', 'LOTOTES41', 'LOTOTES42', 'LOTOTES43', 'LOTOTES44']
但是实际的输出应该是:
['LOTOTES01', 'LOTOTES02', 'LOTOTES03', 'LOTOTES04', 'LOTOTES05', 'LOTOTES06', 'LOTOTES07', 'LOTOTES08', 'LOTOTES09', 'LOTOTES10', 'LOTOTES11', 'LOTOTES12', 'LOTOTES13', 'LOTOTES14', 'LOTOTES15', 'LOTOTES16', 'LOTOTES17', 'LOTOTES18', 'LOTOTES19', 'LOTOTES20', 'LOTOTES21', 'LOTOTES22', 'LOTOTES23', 'LOTOTES24', 'LOTOTES25', 'LOTOTES26', 'LOTOTES27', 'LOTOTES28', 'LOTOTES29', 'LOTOTES30', 'LOTOTES31', 'LOTOTES32', 'LOTOTES33', 'LOTOTES34', 'LOTOTES35', 'LOTOTES36', 'LOTOTES37', 'LOTOTES38', 'LOTOTES39', 'LOTOTES40', 'LOTOTES41', 'LOTOTES42', 'LOTOTES43', 'LOTOTES44']