Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6054315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:08:22+00:00 2026-05-23T08:08:22+00:00

I have a text file that has lines representing some data sets. The file

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:08:22+00:00Added an answer on May 23, 2026 at 8:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large (~8 gb) text file that has very long lines.
I have a simple WPF (XAML) file that has some animated shapes and text.
I have Text file that contains data separated with a comma , . How
I have a text file that contains a long list of entries (one on
I have a CSV-like text file that has about 1000 lines. Between each record
I have a text file with 50k+- lines and each line contains data which
Scenario: I have a text file that has pipe (as in the | character)
I have a rather large text file that has a bunch of missing newlines,
I have a text file that I want to edit using Java. It has
i have a java ee project which has a text file that uses a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.