Python如何在匹配后获取特定数量的行 [英] Python how to grab certain number of lines after match

查看:403
本文介绍了Python如何在匹配后获取特定数量的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下格式的输入文本文件:

Let's say I have an input text file of the following format:

Section1 Heading    Number of lines: n1
Line 1
Line 2
...
Line n1
Maybe some irrelevant lines

Section2 Heading    Number of lines: n2
Line 1
Line 2
...
Line n2

其中文件的某些部分以标题行开头,该标题行指定该部分中的行数.每个部分的标题都有不同的名称.

where certain sections of the file start with a header line that specifies how many lines are in that section. Each section heading has a different name.

我已经写了一个正则表达式,它将根据用户搜索每个节的标题名称匹配标题行,进行解析,然后返回数字n1/n2/etc,告诉我该节中有多少行.我一直在尝试使用for-in循环读取每一行,直到计数器达到n1为止,但到目前为止尚未奏效.

I have written a regular expression that will match the header line based on the header name the user searches for each section, parse it, and then return the number n1/n2/etc that tells me how many lines are in the section. I have been trying to use a for-in loop to read through each line until a counter reaches n1, but it hasn't worked out so far.

这是我的问题:在匹配的行中给出匹配的行号并且每个部分都不同时,我如何只返回匹配行之后的特定行数?我是编程新手,感谢您的帮助.

Here's my question: how do I return just a certain number of lines following a matched line when that number is given in the match and different for each section? I'm new to programming, and I appreciate any help.

好的,这是我到目前为止的相关代码:

Okay, here's the relevant code that I have so far:

import re
print
fname = raw_input("Enter filename: ")
toolname = raw_input("Enter toolname: ")

def findcounter(fname, toolname):
        logfile = open(fname, "r")

        pat = 'SUCCESS Number of lines :'
        #headers all have that format
        for line in logfile:
                if toolname in line:
                    if pat in line:
                            s=line

        pattern = re.compile(r"""(?P<name>.*?)     #starting name
                             \s*SUCCESS        #whitespace and success
                             \s*Number\s*of\s*lines  #whitespace and strings
                             \s*\:\s*(?P<n1>.*)""",re.VERBOSE)
        match = pattern.match(s)
        name = match.group("name")
        n1 = int(match.group("n1"))
        #after matching line, I attempt to loop through the next n1 lines
        lcount = 0
        for line in logfile:
             if line == match:
                    while lcount <= n1:
                                match.append(line)
                                lcount += 1
                                return result

文件本身很长,我感兴趣的部分之间散布着许多无关的行.我不太确定的是如何指定在匹配的行之后直接打印行.

The file itself is pretty long, and there are lots of irrelevant lines interspersed between the sections I'm interested in. What I'm not too sure about is how to specify printing the lines directly after a matched line.

推荐答案

# f is a file object
# n1 is how many lines to read
lines = [f.readline() for i in range(n1)]

这篇关于Python如何在匹配后获取特定数量的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆