[Solved] Reverse block of text every x lines in python


This is working under the assumption that the number of rows between the text is always consistent.

# Open file, split on newlines into a list
with open('file.txt') as f:
    data = f.read().splitlines()
# Break list into multiple lists 7 items long
# this covers the text and the 6 sets of numbers that follow
newlist = [data[i:i + 7] for i in range(0, len(data), 7)]

output = []
# Append the text, then iterate backwards across each list, stopping short of the text
for item in newlist:
    output.append(item[0])
    for x in item[:0:-1]:
        output.append(x)
# output to text file, adding a newline character at the end of each row
with open('newtext.txt', 'w') as f:
    for item in output:
        f.write('{}\n'.format(item))

6

solved Reverse block of text every x lines in python