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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:46:54+00:00 2026-06-04T08:46:54+00:00

I am using Python to read a file of the following format: iter1 iter2

  • 0

I am using Python to read a file of the following format:

iter1
iter2
iter3
[n lines of stuff]
FLAG = value

iter1
iter2
iter3
iter4
iter5
[n lines of stuff]
FLAG = value
etc….

I want to to search for FLAG, read that value, and then rewind back by ‘n’ lines and read the value of the final iteration. Note that there are not always the same number of iterations. The number of lines ‘n’ is consistent within each file; however, these lines may contain different numbers of bytes so I am having trouble using the seek feature.

I would like to do something like this:

f = open(file)  
for i in f:  
    a = re.search('FLAG')  
    if a:  
          print a  
          spot=f.tell() #mark original spot  
          f.seek(-n,1)  #rewind by n lines  
          b = re.search('iter')  
          print b  
          f.seek(spot) #return to FLAG line, continue to next data set  
  • 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-04T08:46:55+00:00Added an answer on June 4, 2026 at 8:46 am

    Assuming your “n lines of stuff” don’t contain any lines starting with “iter”, you’re making the problem much harder than it is. All you need to do is keep track of the last line you saw that started with “iter”. Then when you see “FLAG=” you have that data already; no need to “rewind” and look for it.

    lastiterline = None
    with open(filename) as f:
        for line in f:
            line = line.strip()
            if line.startswith("iter"):
               lastiterline = line
            elif line.startswith("FLAG"):
               if lastiterline:
                   print line
                   print lastiterline
               lastiterline = None
    

    In general, it is simplest to read a file once and remember the bits you’ll need later as they go by.

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

Sidebar

Related Questions

How to Write/Read a file to/from a network folder/share using python? The application will
I am using the csv python package to read a csv file like this:
Using Python, what's the correct way to set a file to be read-only when
How to read a file in reverse order using python? I want to read
I am using Python to generate an ASCII file composed of very long lines.
I'm using urllib (in Python) to fetch an SVG file: import urllib urllib.urlopen('http://alpha.vectors.cloudmade.com/BC9A493B41014CAABB98F0471D759707/-122.2487,37.87588,-122.265823,37.868054?styleid=1&viewport=400x231').read() which
I'm using Python 2.x [not negotiable] to read XML documents [created by others] that
I found that you can't read from some sites using Python's urllib2(or urllib). An
I'm looking for information on how to read GnuCash files using python. I have
I'm trying to copy files inside a Python script using the following code: inf,outf

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.