Simos Anderson
Simos Anderson

Reputation: 297

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

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.

Upvotes: 1

Views: 4392

Answers (1)

g.d.d.c
g.d.d.c

Reputation: 47988

Try something like this instead (slightly pseudocode).

with open('myfile') as fh:
  for line in fh:
    if line == match: # Some actual code here in your conditional:
      for i in range(5):
        additionalData = next(fh)

By calling next(fh) you can retrieve the next line in the file without screwing up your for line in fh loop.

Upvotes: 3

Related Questions