I am using python to do my MapReduce homework which will use sys.stdin as a reader of the input file. for example :
for line in sys.stdin:
# compare 1st line with the 2ed line.
I can load all the file content into memory and use the index to implement a 2 lines comparasion for exmample:
lines= open("guru99.txt","r")
for i in range(len(lines)):
if lines[i] != lines[i-1]:
...
My question is How to do these 2 lines comparison using sys.stdin way? As the homework file "guru99.txt" is huge and I can't load it into memory but only the sys.stdin way will work.
You can get the first line using next
and then iterate the remaining input.
import sys
try:
prev = next(sys.stdin)
except StopIteration:
# no input
exit()
for line in sys.stdin:
if line == prev:
do the things
prev = line