0

I am reading input from sys.stdin in python and I need to perform some extra operations when the last line is encountered. How can I identify if the current line being executed is the last one?

for line in sys.stdin:
    if <line is last line>:
        // do some extra operation
    else:
        // rest of stuff
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
Mohit Verma
  • 2,019
  • 7
  • 25
  • 33
  • Just do whatever you need to do to the last line after the loop. The `line` variable will still be in scope and have the value of the last line read. – tobias_k Apr 21 '15 at 19:42
  • 1
    @tobias_k that will not work, because then `// rest of stuff` will be applied to the last line. Just read one line ahead and operate on the previous line. – tommy.carstensen Apr 21 '15 at 19:43
  • @tommy.carstensen Hm, good point, but then this is not some "extra operation" but an entirely _different_ operation. – tobias_k Apr 21 '15 at 19:44
  • Yes, otherwise it would have been a duplicate of this question: http://stackoverflow.com/questions/24089090 – tommy.carstensen Apr 21 '15 at 19:47

3 Answers3

3

The only way to know that you're at the end of the stream is when you try to read from it and there's nothing there. Logic added after the for-loop will be at the end-of-stream case.

If you need to detect end-of-stream in the input stream, before you've finished with the previous record, then your logic can't use a "for"-loop. Instead, you must use "while." You must pre-read the first record, then, "while" the latest-thing-read isn't empty, you must read the next record and then process the current one. Only in this way can you know, before processing the current record, that there will be no records following it.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
1

Before starting the loop, read the first line of the input. Then, in the loop, always process the line previously read. After the loop terminates, you'll still have the last line from sys.stdin in line_prev for your special processing.

import sys
line_prev = sys.stdin.readline()
for line in sys.stdin:
    rest_of_stuff(line_prev)
    line_prev = line
do_some_extra_operation(line)
chepner
  • 497,756
  • 71
  • 530
  • 681
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
0

Why don't use try this:

for a in iter(raw_input, ""):
    # do something with a

The loop will break when the input equals to the sentinel (the second argument in iter). You can keep a reference to the last line as well, such as:

for a in iter(raw_input, ""):
    if a == last_line:
        # do stuff
    last_line = a
    # Do more stuff

For your understanding, all input in python is taken from sys.stdin, and as a result, you can use functions such as raw_input, and it will read from sys.stdin. Think of it like this:

def raw_input():
    sys.stdin.readline()

It's not exactly like that, but it's similar to that concept.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66