-1

i'm looking to create a new CSV file from the first file, but the last line in the first file is the first line in the second file and so forth, until all the lines/rows are reversed.

cheers Jon

kastin
  • 25
  • 4
  • 1
    does it need to be done in python? Why not just a [shell command](http://stackoverflow.com/questions/742466/how-can-i-reverse-the-order-of-lines-in-a-file)? e.g. `tail -r data.csv`? – hansaplast Jan 10 '17 at 12:08

1 Answers1

0

Using python only:

with open('data.csv', 'r', encoding='utf-8') as f:
    lines = f.readlines()

with open('reversed_data.csv', 'w+', encoding='utf-8') as f:
    for l in reversed(lines):
        f.write(l)

Or via shell:

tail -r data.csv > reverse_data.csv
deepbrook
  • 2,523
  • 4
  • 28
  • 49
  • Perfect, thank you. i had been stuffing around with skip and nrows, and knew there was a much simpler way. – kastin Jan 12 '17 at 11:26