Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly get the last line from a .csv file over a network drive?

I store thousands of time series in .csv files on a network drive. Before I update the files, I first get the last line of the file to see the timestamp and then I update with data after that timestamp. How can I quickly get the last line of a .csv file over a network drive so that I don't have to load the entire huge .csv file only to use the last line?

like image 526
user1367204 Avatar asked Jun 21 '17 16:06

user1367204


1 Answers

There is a nifty reversed tool for this, assuming you are using the built-in csv module:

how to read a csv file in reverse order in python

In short:

import csv
with open('some_file.csv', 'r') as f:
    for row in reversed(list(csv.reader(f))):
        print(', '.join(row))

In my test file of:

1:   test, 1
2:   test, 2
3:   test, 3

This outputs:

test, 3
test, 2
test, 1
like image 109
Douglas Avatar answered Oct 04 '22 23:10

Douglas