3 回答
TA贡献1818条经验 获得超11个赞
您可以通过多种方法进行此操作。最简单的方法可能是在if语句之前初始化一个空字符串。然后,不要打印split [1]和ip [2],而是将它们连接到空字符串,然后打印。所以看起来像这样:
printstr = ""
if re.search...
...
printstr += "Label for first item " + split[1] + ", "
if re.search...
...
printstr += "Label for second item " + ip[2]
print printstr
TA贡献1827条经验 获得超8个赞
您还可以使用标记curhost,并填充字典:
with open("dhcpd.conf","r") as dhcp_file:
curhost,hosts=None,{}
for line in dhcp_file:
if curhost and '}' in line: curhost=None
if not curhost and re.search(r'^\s*host\b',line):
curhost=re.split(r'\s+', line)[1]
hosts[curhost] = dict()
if curhost and 'hardware ethernet' in line:
hosts[curhost]['ethernet'] = line.split()[-1]
print hosts
添加回答
举报