3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
您可以通過多種方法進(jìn)行此操作。最簡(jiǎn)單的方法可能是在if語句之前初始化一個(gè)空字符串。然后,不要打印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貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
您還可以使用標(biāo)記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
添加回答
舉報(bào)