2017-09-23 69 views
0

我有几个文件需要从中提取某些信息: 文件内容的示例(并非我已经屏蔽了IP)。每个文件可以是约15K线,下面的示例内容:来自文本文件的报告 - python

(*, 224.0.0.50/32), uptime: 27w6d, igmp ip pim      
    Incoming interface: Ethernet1/36, RPF nbr: 1.1.1.2, uptime: 1w4d 
    Outgoing interface list: (count: 3)        
    Ethernet1/47, uptime: 1w5d, pim        
    Vlan25, uptime: 7w4d, igmp          
    Vlan20, uptime: 27w6d, igmp         

(1.1.1.1/32, 224.0.0.50/32), uptime: 09:51:59, ip mrib pim    
    Incoming interface: Ethernet1/36, RPF nbr: 1.1.1.2, uptime: 09:51:59 
    Outgoing interface list: (count: 3)         
    Ethernet1/47, uptime: 09:51:59, pim        
    Vlan20, uptime: 09:51:59, mrib          
    Vlan25, uptime: 09:51:59, mrib 

我需要做的是通过文件和打印运行如下:

Source IP Group IP  Incoming Interface  Outgoing Interface 
1.1.1.1 224.0.0.50 Ethernet1/36   Vlan20, Vlan25 

我写的是这样的:

import re 

mroute = open("multicast.txt", 'r') 

for line in mroute: 
    if re.match("(.*)(\()1(.*)", line): 
     print line 
for line in mroute: 
    if re.match("(.*)(In)(.*)",line): 
     print line 
for line in mroute: 
    if re.match("(^)(Out)(.*)",line): 
     print line 

然而,每个部分独立工作时,我加入他们不显示任何东西。

回答

0

我相信你的问题是你的每个for line in mroute耗尽了迭代器。一个更好的方法是收集一组线并按照处理方式处理它们。

这里是什么,我会先从

import sys, re 

class Details(dict): 
    _format = '%(source)-20s %(group)-20s %(incoming)-20s %(outgoing)-20s' 
    header = _format % { 
     'source':'Source IP', 
     'group': 'Group IP', 
     'incoming': 'Incoming Interface', 
     'outgoing': 'Outgoing Interface' 
    } 
    def __repr__(self): 
     return self._format % self 

    def __init__(self, g): 
     super(Details, self).__init__() 
     self.g = g 
     self.parse_line(0, "^[(](?P<source>[^,]+), (?P<group>[^)]+)") 
     self.parse_line(1, "^ Incoming interface: (?P<incoming>[^,]+),") 
     self.parse_line(2, "^ Outgoing interface list: (?P<unused>.+)") 
     for l in self.g[3:]: 
      p = "^ ([^,]+)," 
      m = re.search(p, l) 
      if m: 
       o = self.get('outgoing','') 
       if o: o += ', ' 
       self['outgoing'] = o + m.group(1) 

    def parse_line(self, n, p, u='?'): 
     r = re.compile(p) 
     e = {x:u for x in r.groupindex} 
     m = r.search(self.g[n]) 
     d = m.groupdict() if m else e 
     self.update(d) 

    @staticmethod 
    def parse(lines): 
     groups = [[]] 
     for line in lines: 
      if line.startswith("("): 
       groups.append([]) 
      groups[-1].append(line) 
     return [Details(g) for g in groups if g] 

print Details.header 
for d in Details.parse(file(sys.argv[1])): 
    print d 

采样运行

Source IP   Group IP    Incoming Interface Outgoing Interface 
*     224.0.0.50/32  Ethernet1/36   Ethernet1/47, Vlan25, Vlan20 
1.1.1.1/32   224.0.0.50/32  Ethernet1/36   Ethernet1/47, Vlan20, Vlan25 

这不处理不规则数据或接口 的语义但这些都是很容易地添加,如果你知道是什么你要。

+0

这很棒,但是看起来这是对我的头脑,我为此道歉。我如何将我的文件添加到此...? 另外我得到某种错误代码IndexError:列表索引超出范围,当您在此行发布的“干运行”代码:for Details.parse(file(sys.argv [1])): 谢谢这真是太神奇了...... – gargolek

+0

我将它作为'$ python test.py sample'运行,其中'test.py'包含程序,'sample'包含示例数据。 – jq170727

+0

再次感谢您的道歉,您必须像我一样对待新手。 这样的工作,它让我的文件中,然后返回下面的错误代码: 回溯(最近通话最后一个): 文件“C:\用户\ Gargolek \ Multicast_FILE \ Multicast_v4.py ”第47行,在 打印d 文件 “C:\用户\ Gargolek \ Multicast_FILE \ Multicast_v4.py”,第13行,在__repr__ 返回self._format%自 KeyError异常: '呼出' – gargolek