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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:36:06+00:00 2026-05-20T04:36:06+00:00

I run across this problem frequently suppose I have a text file that I

  • 0

I run across this problem frequently suppose I have a text file that I have read in as as a list using file.readlines()

suppose the file looks something like this

stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff #indeterminate number of line \
The text I want is set off by something distinctive
I want this
I want this
I want this
I want this # indeterminate number of lines
The end is also identifiable by something distinctive
stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff
stuff stuff stuff stuff stuff

The way I have been handling this is to do something like this

themasterlist=[]
for file in filelist:
    count=0
    templist=[]
    for line in file:
        if line=='The text I want is set off by something distinctive':
            count=1
        if line=='The end is also identifiable by something distinctive':
            count=0
        if count==1:
        templist.append(line)
   themasterlist.append(templist)

I have thought about using the string (file.read()) and splitting it based on the end points and then converting it to a list but actually I want to use this construction for a number of other types. For example, suppose I am iterating through the elements of an lxml.fromstring(somefile) and I want to process a subset of the elements based on whether or not the element.text contains some phrase etc.

Note, I could be running through 200K to 300K files at a time.

My solution works but it feels clunky and like I am missing something important about python

There are three really good answers and I learned something useful from each. I need to select one as the answer but I do appreciate the response of each poster it was very helpful

  • 1 1 Answer
  • 3 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-20T04:36:07+00:00Added an answer on May 20, 2026 at 4:36 am

    I like stuff like this:

    def findblock( lines, start, stop ):
        it = iter(lines)
        for line in it:
            if start in line:
                # now we are in the block, so yield till we find the end
                for line in it:
                    if stop in line:
                        # lets just look for one block
                        return # leave this generator
                        # break # would keep looking for the next block
                    yield line                
    
    for line in findblock(lines, start="something distinctive", 
                                 stop="something distinctive"):
        print line
    

    The stuff you were missing is yield and list comprehensions – here is your code revised:

    def findblock( lines, start='The text I want is set off by something distinctive', 
                          stop='The end is also identifiable by something distinctive'):
        for line in lines:
            inblock = False
            if line==start:
                inblock=True
            if line==stop:
                inblock=False # or return mb?
            if inblock:
                yield line
    
    themasterlist = [list(findblock( file )) for file in files]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into this problem across multiple programming languages and I was just
One of the patterns that I frequently run across when developing is trying to
So I've run across a problem that has been addressed in other languages but
I've just run across an annoying feature of the Client-Side Validation that I'm wondering
When validating user inputs in an application that is to be run across multiple
I'm working on writing a Pong game and I've run across a problem. I'm
In all my years of .NET programming I have not run across a bug
In the last two days, ive run across code that has php echo'd variables
While building several different projects in QtCreator, I have run across the following build
Today I wanted to perform this task, and I run across some issues in

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.