Python打印(项)不显示任何输出

2024-04-25 06:44:48 发布

您现在位置:Python中文网/ 问答频道 /正文

第一次print(item)我得到了我想要的结果。但是在第二个print(item),(查找IP的正则表达式)不打印任何内容。我做错了什么

代码基本上是在文本文件中搜索其中的所有IP。 然后我尝试在显示ip之前添加一个字符串

txt文件:

                               IP Arp - ITF Omn-tool-A
==========================================================================================
IP_ADDRESS      MAC_ADDRESS        VLAN  PORT                 TYPE    TTL(10 Sec) TUNNEL
------------------------------------------------------------------------------------------
192.168.0.1     ff:ff:ff:ff:ff:ff  -     -                    LOCAL   2160
192.168.0.2       ff:ff:ff:ff:ff:ff  103   -                    LOCAL 2160
192.168.0.3       ff:ff:ff:ff:ff:ff  103  -                    LOCAL  2160
192.168.0.4     ff:ff:ff:ff:ff:ff  103   c657:1/45            DYNAMIC 1031
192.168.0.5     ff:ff:ff:ff:ff:ff  103   c657:1/46            DYNAMIC 877
192.168.0.6     ff:ff:ff:ff:ff:ff  103   c657:1/45            DYNAMIC 2149
192.168.0.7    ff:ff:ff:ff:ff:ff  103   -                    LOCAL   2160

====================================================================================================
                        IP Arp Extn - ITF Omn-toll-A
====================================================================================================
MULTICAST-MAC-FLOODING    AGING(Minutes)       ARP-THRESHOLD
----------------------------------------------------------------------------------------------------
N/A                       360                  N/A

c: customer vid   u: untagged-traffic
7 out of 68 ARP entries displayed
acli.pl: Displayed Record Count = 8
ARPs on TX-NNI: Current = 0, re-ARP count = 0
==========================================================================================
                                  IP Arp - ITF Omn-b
==========================================================================================
IP_ADDRESS      MAC_ADDRESS        VLAN  PORT                 TYPE    TTL(10 Sec) TUNNEL
------------------------------------------------------------------------------------------
192.168.0.8     ff:ff:ff:ff:ff:ff  -     -                    LOCAL   2160
192.168.0.9       ff:ff:ff:ff:ff:ff  103   -                    LOCAL 2160
192.168.0.10       ff:ff:ff:ff:ff:ff  103   -                    LOCAL2160
192.168.0.11      ff:ff:ff:ff:ff:ff  103   c658:1/45           DYNAMIC1015
192.168.0.12    ff:ff:ff:ff:ff:ff  103   c658:1/46            DYNAMIC 878
192.168.0.13    ff:ff:ff:ff:ff:ff  103   c658:1/16            DYNAMIC 873
192.168.0.14     ff:ff:ff:ff:ff:ff  103   c658:1/17            DYNAMIC 873
192.168.0.15     ff:ff:ff:ff:ff:ff  103   c658:1/45            DYNAMIC1017
192.168.0.16     ff:ff:ff:ff:ff:ff  103   c658:1/46            DYNAMIC 873
192.168.0.17     ff:ff:ff:ff:ff:ff  103   c658:1/46            DYNAMIC 876

----

我希望输出如下:

IP Arp - ITF Omn-tool-A-192.168.0.1                     
IP Arp - ITF Omn-tool-A-192.168.0.2       
IP Arp - ITF Omn-tool-A-192.168.0.3       
IP Arp - ITF Omn-tool-A-192.168.0.4     
IP Arp - ITF Omn-tool-A-192.168.0.5    
IP Arp - ITF Omn-tool-A-192.168.0.6     
IP Arp - ITF Omn-tool-A-192.168.0.7


IP Arp - ITF Omn-b-192.168.0.8     
IP Arp - ITF Omn-b-192.168.0.9       
IP Arp - ITF Omn-b-192.168.0.10       
IP Arp - ITF Omn-b-192.168.0.11     
IP Arp - ITF Omn-b-192.168.0.12   
IP Arp - ITF Omn-b-192.168.0.13    
IP Arp - ITF Omn-b-192.168.0.14     
IP Arp - ITF Omn-b-192.168.0.15     
IP Arp - ITF Omn-b-192.168.0.16   
IP Arp - ITF Omn-b-192.168.0.17  

这是我的密码:

with open(Sw0, 'r') as f:
     for line1 in f:

          #if in_file:
                line1 = line1.split(' ')

                for item in line1:
                    if re.search(r'^[a-zA-Z]+\_[a-zA-Z]+', item):
                      match = re.search(r'[a-zA-Z]+\_[a-zA-Z]+', item)
                      data0.append(match.group(0))

                for item in f:
               # section header
                    match = re.search(r'IP Arp [^Extn-]*-\s*(\w+)', item)
                    if match is not None:
                       section = match.group(0)
                       print(item)
            #print(section)
                       continue

                       for item in f:
                           match = re.search(r'^\d+\.\d+\.\d+\.\d+', item)
                           print(item)
                   
                           if match is not None:
                              data5.append({'section': section, 'ip': match.group(0)})
                              print(item)
                              for d in data5:
                                  print('{} {}'.format(d['section'], d['ip']))


Tags: iniprelocalmatchsectiondynamictool
1条回答
网友
1楼 · 发布于 2024-04-25 06:44:48

您不需要读取文件f两次。只需迭代一次,如果您找到一个报头,则更新当前报头并查找其下的所有IP地址。我们仍然可以在这里使用continue返回到for line in f查找报头,因为不需要在当前行上搜索IP地址。我们也不需要一直打电话给re.search。只需先检查它是否不是None,然后调用.group(0)

另外,我认为^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}是一种更适合搜索IPv4地址的模式,因为每个八位组只能有0到255之间的数字。我还使用了IP Arp .*模式来查找头,因为头似乎需要以IP Arp开头,并且后面可以是任何字符,因此.*

以下是您可以做的:

from re import search

with open("data.txt") as f:

    header = None
    for line in f:
        header_match = search(r'IP Arp .*', line)
        if header_match:
            header = header_match.group(0)
            continue

        ip_address = search(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line)
        if ip_address:
            print(f"{header}-{ip_address.group(0)}")

输出:

IP Arp - ITF Omn-tool-A-192.168.0.1
IP Arp - ITF Omn-tool-A-192.168.0.2
IP Arp - ITF Omn-tool-A-192.168.0.3
IP Arp - ITF Omn-tool-A-192.168.0.4
IP Arp - ITF Omn-tool-A-192.168.0.5
IP Arp - ITF Omn-tool-A-192.168.0.6
IP Arp - ITF Omn-tool-A-192.168.0.7
IP Arp - ITF Omn-b-192.168.0.8
IP Arp - ITF Omn-b-192.168.0.9
IP Arp - ITF Omn-b-192.168.0.10
IP Arp - ITF Omn-b-192.168.0.11
IP Arp - ITF Omn-b-192.168.0.12
IP Arp - ITF Omn-b-192.168.0.13
IP Arp - ITF Omn-b-192.168.0.14
IP Arp - ITF Omn-b-192.168.0.15
IP Arp - ITF Omn-b-192.168.0.16
IP Arp - ITF Omn-b-192.168.0.17

相关问题 更多 >