0

I'm doing an exercise that takes in a file with data of server room temperatures and I want to print out all of the average temperatures of all the years. So I need to get the next line in the file to determine what the next year is. I tried next() but that forces the for loop to go to the next iteration. I need the 1st and 2nd line then move to the 2nd line and get the 3rd line, etc.

Here is the portion of code:

with open("THUMlog.txt", "r") as file:
for line in file:
    if line == "#DATE TIME TEMPERATURE UNIT HUMIDITY%\n":
        continue
    stripped_line = line.rstrip().split()
    next_line = next(file, "").rstrip().split()
    print(f"Stripped: {stripped_line}")
    print(f"Next: {next_line}")

Portion of Output (Very large file):

    Stripped: ['9/28/2010', '15:45:14', '22.248900', 'C', '44.721968']
Next: ['9/28/2010', '16:00:07', '22.738900', 'C', '50.539993']
Stripped: ['9/28/2010', '16:15:07', '23.388900', 'C', '49.339338']
Next: ['9/28/2010', '16:30:07', '23.918900', 'C', '47.539280']
Stripped: ['9/28/2010', '16:45:08', '23.668900', 'C', '40.700378']
Next: ['9/28/2010', '17:00:07', '23.438900', 'C', '40.130996']
Stripped: ['9/28/2010', '17:15:07', '23.188900', 'C', '40.546257']
Next: ['9/28/2010', '17:30:07', '23.038900', 'C', '40.361183']
Stripped: ['9/28/2010', '17:45:07', '22.978900', 'C', '40.356943']
Next: ['9/28/2010', '18:00:08', '22.808900', 'C', '40.239134']
Stripped: ['9/28/2010', '18:15:07', '22.748900', 'C', '40.329383']
Next: ['9/28/2010', '18:30:07', '22.588900', 'C', '40.347069']

So basically, The first Next should be the same list as the second Stripped. I tried concatenating all these lists into a list so I could easily get the next one through indexing but I get time limit exceeded probably because I had 2 for loops, one looping through the entire file, splitting the line into a list, and appending to another list. Then another looping through the list of list. This effectively doubles the amount of time. This is a bonus exercise in my programming course and is done in software that runs and checks your solution.

7
  • are you trying to read 2 steps every time? Commented Jan 18, 2022 at 18:00
  • Read the whole file in memory as an array of lines before processing it : with open("file.txt") as f: content = f.readlines() Commented Jan 18, 2022 at 18:00
  • 1
    You could keep two variables, one that contains the current line and one that contains the previous line. When you are ready to move on, put the current line in the previous line, then read the next line. Commented Jan 18, 2022 at 18:01
  • The code you post doesn't seem to actually need the next line. Perhaps you could update your code with something that acts as though you have the next line and performs some appropriate processing. Commented Jan 18, 2022 at 18:01
  • @XxJames07- Yes I want to read the first line and second line, then the second line and third line, then third line and fourth line, etc. Commented Jan 18, 2022 at 18:03

3 Answers 3

1

You are using a single iterator both in a loop and using next. That can get confusing. Think about it:

  • at iteration i, line is line k and next_line is line k+1.
  • at iteration i+1, line is now line k+2.

You need to pass/save one line between iterations. Using a sample file of consecutive numbers in each line:

1
2
3
...

Using the code:

with open("text.txt") as file:
    line = next(file)
    for next_line in file:
        print(line.strip())
        print(next_line.strip())
        print("---")
        line = next_line

Will give:

1
2
---
2
3
---
3
4
---
...
Sign up to request clarification or add additional context in comments.

6 Comments

I think this will work for me thank you I'll try that now.
Hmm, I still only get the first line on the first iteration: 9/28/2010 15:45:14 22.248900 C 44.721968 --- 9/28/2010 15:45:14 22.248900 C 44.721968 9/28/2010 16:00:07 22.738900 C 50.539993 --- 9/28/2010 16:00:07 22.738900 C 50.539993 9/28/2010 16:15:07 23.388900 C 49.339338
Nvm I'm an idiot lol
So I want to omit the first line as you can see in the code I provided in my original post, would the method I used still work?
What do you mean? The part with if line == "#DATE TIME T...? If that is to skip the first line you can do it much simpler. Just add another next(file) right after the with. That will simply throw-away the first line
|
1

You could simply track the previous line, separately, at update at the end of each iteration:

prev_line = None
with open("THUMlog.txt", "r") as file:
    for line in file:
        if line == "#DATE TIME TEMPERATURE UNIT HUMIDITY%\n":
            continue
        stripped_line = line.rstrip().split()
        if prev_line:
            print(f"Stripped: {prev_line}")
            print(f"Next: {line}")
        prev_line = line

Comments

0

You can do this by rewinding the position within the current file.

with open(...) as fh:
    pos = fh.tell() # This is where we want to go back to
    # Do what ever you want such as read lines.
    if INEEDTOGOBACK:
        fh.seek(pos)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.