def split_generator(s):
    start_prev = end_prev = 0
start = end = 0
start_next = end_next = 0
nr_lines = 0
for idx, c in enumerate(s):
    if c == '\n':
        nr_lines += 1
start_prev = start
end_prev = end
start = start_next
end = end_next
start_next = end_next
end_next = idx
if nr_lines >= 3:
    yield s[start + 1:end]
if s[-1] != '\n' and nr_lines >= 2:
    yield s[start_next + 1:end_next]
Fastest way to remove first and last lines from a Python string