第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用python在一行中打印多個(gè)變量

使用python在一行中打印多個(gè)變量

梵蒂岡之花 2021-03-28 10:54:25
我需要有關(guān)python腳本的一些幫助。我需要在dhcpd文件中搜索主機(jī)整體,其MAC和IP,并將其打印在一行中。我能夠找到主機(jī)名和IP地址,但無法弄清楚如何將if語句中的變量放入一行中。任何建議,代碼如下:#!/usr/bin/pythonimport sysimport re#check for argumentsif len(sys.argv) > 1:    print "usage: no arguments required"    sys.exit()else:    dhcp_file = open("/etc/dhcp/dhcpd.conf","r")    for line in dhcp_file:        if re.search(r'\bhost\b',line):            split = re.split(r'\s+', line)            print split[1]        if re.search(r'\bhardware ethernet\b',line):            ip = re.split(r'\s+',line)            print ip[2]    dhcp_file.close()
查看完整描述

3 回答

?
慕尼黑8549860

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


查看完整回答
反對(duì) 回復(fù) 2021-04-02
?
斯蒂芬大帝

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


查看完整回答
反對(duì) 回復(fù) 2021-04-02
  • 3 回答
  • 0 關(guān)注
  • 379 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)