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 8323489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:44:15+00:00 2026-06-08T23:44:15+00:00

I am having trouble getting python to read specific lines. What i’m working on

  • 0

I am having trouble getting python to read specific lines. What i’m working on is something like this:

lines of data not needed
lines of data not needed
lines of data not needed

--------------------------------------
    ***** REPORT 1 *****
--------------------------------------

[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here      #This can also be the EOF

--------------------------------------    
    ***** REPORT 2 *****
--------------------------------------

lines of data not needed
lines of data not needed
lines of data not needed         #Or this will be the EOF

What I’ve attempted was something such as:

flist = open("filename.txt").readlines()

for line in flist:
  if line.startswith("\t**** Report 1"):
    break
for line in flist:
  if line.startswith("\t**** Report 2"):
    break
  if line.startswith("[key]"):
    #do stuff with data

However, I have a problem when the file ends without a end delimiter… Such as when report #2 is not displayed. What is a better approach?

  • 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-06-08T23:44:16+00:00Added an answer on June 8, 2026 at 11:44 pm

    One slight modification which looks like it should cover your problem:

    flist = open("filename.txt").readlines()
    
    parsing = False
    for line in flist:
        if line.startswith("\t**** Report 1"):
            parsing = True
        elif line.startswith("\t**** Report 2"):
            parsing = False
        if parsing:
            #Do stuff with data 
    

    If you want to avoid parsing the line “* Report 1″… itself, simply put the start condition after the if parsing, i.e.

    flist = open("filename.txt").readlines()
    
    parsing = False
    for line in flist:
    
        if line.startswith("\t**** Report 2"):
            parsing = False
        if parsing:
            #Do stuff with data 
        if line.startswith("\t**** Report 1"):
            parsing = True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning python and am having trouble getting this program to work correctly.
I'm having trouble getting my python data to write as csvs, either using pylab's
I am having some trouble getting Python IO redirected to a console that I've
I'm having trouble getting command line arguments passed to Python programs if I try
I'm having trouble getting Tire working using ElasticSearch with the Bonsai addon on the
I'm just getting started with twisted.web, and I'm having trouble importing a Python module
I'm having troubles getting this to work. Basically I have a python program that
I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar
I am having trouble getting PYTHONPATH set. I have added this line to ~/.bashrc
I'm working my way through DiveIntoPython.com and I'm having trouble getting the import to

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.