0

我试图弄清楚如何从这个文本文件中获取每个 AP 行上的客户端数量,这对于那些与它关联的“客户端数量”(在下一行)来说似乎很好,但是您可以查看是否没有客户端与之关联,它不会打印大量客户端。

我无法弄清楚检查下一行以查看是否存在“客户数量”的逻辑,然后返回当前行。如果它存在,它应该继续到下一行并获取客户编号。如果没有“客户数量”行,我正在尝试将客户设置为 0。

我有一个包含以下内容的文件:

无线-detail.txt

RUDY>show wireless ap detail on AP1 | include clients
RUDY>show wireless ap detail on AP2 | include clients
 Num of clients       : 8  
RUDY>show wireless ap detail on AP3 | include clients
 Num of clients       : 21
RUDY>show wireless ap detail on AP4 | include clients
RUDY>show wireless ap detail on AP5 | include clients
 Num of clients       : 2

现在我有以下内容:

for line in file:   
    if "AP" in line:
        ap = re.search('AP[0-99]+', line)
        print ap.group(0),
    elif "Num of clients" in line:  
        clients = re.search('[0-99]+', line)
        print '- ' + clients.group(0)

它当前打印以下内容:

AP1 AP2 - 8
AP3 - 21
AP4 AP5 - 2
AP6 - 5
AP7 - 2
AP8 - 5
AP9 - 5

让它检查下一行以查看 AP 是否应设置为 0 个客户端的最佳方法是什么?

编辑:FWIW - 我正在尝试 file.next() 读取下一行,这似乎有效,但我无法回到上一行:/

编辑#2:我希望我能投票给你们所有人。谢谢大家!令人难以置信的是有这么多方法可以做到这一点,我无法弄清楚其中的一种!!!!

4

5 回答 5

2

这是一个使用正则表达式的简短方法。注意 re.MULTILINE 标志

s='''RUDY>show wireless ap detail on AP1 | include clients
RUDY>show wireless ap detail on AP2 | include clients
 Num of clients       : 8  
RUDY>show wireless ap detail on AP3 | include clients
 Num of clients       : 21
RUDY>show wireless ap detail on AP4 | include clients
RUDY>show wireless ap detail on AP5 | include clients
 Num of clients       : 2'''
import re
print re.findall(r'(AP\d) \| include clients(?:$\n Num of clients {7}: (\d))?',s,flags=re.M)

使(?:$\n Num of clients {7}: (\d))?一个非捕获组,并且与?最后,它是可选的。如果它没有捕获,第二个匹配组将为空,就像 1 和 4 一样。

意味着 7个" {7}"空格

打印这个:

  [('AP1', ''), ('AP2', '8'), ('AP3', '2'), ('AP4', ''), ('AP5', '2')]
于 2012-04-03T22:29:16.570 回答
1

如果您希望数据采用更结构化的格式(例如字典),可以尝试以下方法:

with open('wireless-detail.txt', 'r') as fp:
    access_points = {}
    ap = None
    for line in fp:
        if 'AP' in line:
            ap = line[line.find('AP'):line.find('|')].strip()
            access_points[ap] = 0
        elif "Num of clients" in line:
            access_points[ap] = int(line.split(':')[1].strip())

print access_points

回报:

{'AP2': 8, 'AP3': 21, 'AP1': 0, 'AP4': 0, 'AP5': 2}

我同意以前的解决方案,re这对于此类任务来说是不必要的复杂化,因为您的文件是可靠输出的。这种方法的一个好处是您还可以获得关于没有连接用户的已知接入点的信息,例如,AP1 = 0(也是 int() 形式!)

于 2012-04-03T22:39:52.493 回答
0

you're overthinking it. Don't use regular expressions. they're slow, buggy, and you're pattern is very well defined. Do something like...

for line in file:
    if "AP" in line:
        i = line.find('AP')
        splitLine = line[i+2:].split('|')
        val = splitLine[0]
        print val,
    elif "Num of clients" in line:
        splitLine = line.split(':')
        num = splitLine[1]
        print '- ' + num
于 2012-04-03T21:28:30.360 回答
0

如果你打算使用 RE,你可以做这样的事情。变量用于确定是否需要计数线。如果计数线可用,则使用它,否则将其设置为- 0

need_count = 0
for line in file:   
    if "AP" in line:
        if need_count:
           print '- 0'
        ap = re.search('AP[0-99]+', line)
        print ap.group(0)
        need_count = 1
    elif "Num of clients" in line:  
        clients = re.search('[0-99]+', line)
        print '- ' + clients.group(0)
        need_count = 0
于 2012-04-03T21:31:16.007 回答
0

这可能是矫枉过正,但这里......

我创建了一个库,其中包含一些经常派上用场的函数:

def fileLineGroups (f, count=2, truncate=True):
    lines = []
    for line in f:
        lines.append(line)
        if len(lines) == count:
            yield lines
            lines = lines[1:]
    if not truncate:
        if lines:
            yield lines

这将遍历打开的文件句柄的行,产生行组。它默认为 2 组,因此它将返回 [line1, line2],然后是 [line2, line3] 等。如果您保持 truncate on,如果其中没有 count 行,它将不会返回最终组. for a, b in fileLineGroups(f)如果它是一个奇数,这可以让你做一些事情,而不会在最后一个错误上出错。

现在您可以执行以下操作:

import re
def getAPClientCounts (filepath):
    with open(filename) as f:
        for line1, line2 in py.fileLineGroups(f):
            match1 = re.search('AP\d*', line1)
            if match1:
                match2 = re.search('Num of clients.*: (\d*)', line2)
                if match2:
                    yield match1.group(), match2.groups()[0]

for ap, count in getAPClientCounts('wireless-detail.txt'):
    print 'AP with name %s has %s clients' % (ap, count)

AP with name AP2 has 8 clients
AP with name AP3 has 21 clients
AP with name AP5 has 2 clients
于 2012-04-03T22:42:22.413 回答