Python File Search Line And Return Specific Number of Lines after Match

Posted by Simos Anderson on Stack Overflow See other posts from Stack Overflow or by Simos Anderson
Published on 2011-06-21T20:08:12Z Indexed on 2011/06/22 0:22 UTC
Read the original article Hit count: 259

Filed under:
|
|
|
|

I have a text file that has lines representing some data sets. The file itself is fairly long but it contains certain sections of the following format:

Series_Name                INFO Number of teams : n1
|    Team                                      |     #     |    wins     |
|    TeamName1                                 |     x     |    y        |
.
.
.
|    TeamNamen1                                |     numn  |    numn     |
Some Irrelevant lines
Series_Name2               INFO Number of teams : n1
|    Team                                      |     #     |    wins     |
|    TeamName1                                 |     num1  |    num2     |
.

where each section has a header that begins with the Series_Name. Each Series_Name is different. The line with the header also includes the number of teams in that series, n1. Following the header line is a set of lines that represents a table of data. For each series there are n1+1 rows in the table, where each row shows an individual team name and associated stats. I have been trying to implement a function that will allow the user to search for a Team name and then print out the line in the table associated with that team. However, certain team names show up under multiple series. To resolve this, I am currently trying to write my code so that the user can search for the header line with series name first and then print out just the following n1+1 lines that represent the data associated with the series. Here's what I have come up with so far:

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

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

        pat = 'INFO Number of teams :'

        for line in logfile:
                if seriesname in line:
                    if pat in line:
                            s=line

        pattern = re.compile(r"""(?P<name>.*?)     #starting name
                             \s*INFO        #whitespace and success
                             \s*Number\s*of\s*teams  #whitespace and strings
                             \s*\:\s*(?P<n1>.*)""",re.VERBOSE)
        match = pattern.match(s)


        name = match.group("name")
        n1 = int(match.group("n1"))
        print name + " has " + str(n1) + " teams"
        lcount = 0

        for line in logfile:
                if line.startswith(name):
                        if pat in line:
                                while lcount <= n1:
                                        s.append(line)
                                        lcount += 1
                                        return result

The first part of my code works; it matches the header line that the person searches for, parses the line, and then prints out how many teams are in that series. Since the header line basically tells me how many lines are in the table, I thought that I could use that information to construct a loop that would continue printing each line until a set counter reached n1. But I've tried running it, and I realize that the way I've set it up so far isn't correct. So here's my question: How do you return a number of lines after a matched line when given the number of desired lines that follow the match? I'm new to programming, and I apologize if this question seems silly. I have been working on this quite diligently with no luck and would appreciate any help.

© Stack Overflow or respective owner

Related posts about python

Related posts about file