Categories
Mastering Development

Reorder Lines in a Text File (Loop Assistance)

A have a fairly large text file that I need to reorder the lines such as….

line1
line2
line3

Reordered to look like this

line2
line1
line3

File will continue with more lines and the same reordering must occur. I’m stuck and need a loop to do this. Unfortunately, I have hit a speed bump.

with open('poop.txt') as fin, open('clean330.txt', 'w') as fout:
     for line in fin:
          ordering = [1, 0, 2]
          for idx in ordering: # Write output lines in the desired order.
               fout.write(line)

Leave a Reply

Your email address will not be published. Required fields are marked *