You can write a generator that returns everything except the last entry in the input iterator:
def skip_last(iterator): prev = next(iterator) for item in iterator: yield prev prev = item
then wrap the CSV_raw reader CSV_raw with:
for row in skip_last(CSV_raw):
The generator basically takes the first record, then starts the loop and at each iteration displays the previous record. When the input iterator is completed, there remains one more line that never returns.
The general version, allowing to skip the last elements of n , will be:
from collections import deque from itertools import islice def skip_last_n(iterator, n=1): it = iter(iterator) prev = deque(islice(it, n), n) for item in it: yield prev.popleft() prev.append(item)
source share