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

  • Home
  • SEARCH
  • 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 7429067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:54:39+00:00 2026-05-29T08:54:39+00:00

I am reading through a file using a for loop like this… f =

  • 0

I am reading through a file using a for loop like this…

f = open("somefile.txt")

for line in f:
    do stuff

except for each line I read i need to take an item from the line ahead of it and put it in the current line. What is the best way to do this? Is there a way to read the next line or get some item from it without reading it?

  • 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-29T08:54:40+00:00Added an answer on May 29, 2026 at 8:54 am

    If my understanding is correct, and you want to work on each line in turn, using some value from the next line, my suggestion would be simply to store the value you are currently reading, and work on the last value. Work in reverse – the last_line is your current line and line is the next one.

    last_line = None
    
    with open("somefile.txt") as f:
        for line in f:
            if not last_line == None:
                do_stuff(last_line, extract_needed_part(line))
            last_line = line
    do_stuff(last_line) #The final line without anything following it.
    

    In mathematical terms, instead of line n and line n+1, do line n-1 and line n. Same effect.

    The upside to this method is it doesn’t mean loading the entire file at the beginning.

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

Sidebar

Related Questions

Ok, I'm reading through a file now, line by line. I know each functions
I usually loop through lines in a file using the following code: open my
I'm using a CSV dataset config element, which is reading from a file like
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
I'm reading positional records from a text file, for examle, it looks like this:
I am reading through a file using a batch script. Basically I want to
I'm initially building an index-like mechanism, read each line of the textfile using getline,
Reading through this question on multi-threaded javascript, I was wondering if there would be
I am reading a text file character by character using ifstream infile.get() in an
I'm reading in a text file using BinaryReader, then doing what I want with

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.