如何通过网络驱动器快速从.csv文件中获取最后一行?

时间:2017-06-21 16:46:03

标签: python csv pandas network-programming python-os

我在网络驱动器上的.csv个文件中存储了数千个时间序列。在更新文件之前,我首先获取文件的最后一行以查看时间戳,然后在该时间戳之后更新数据。如何通过网络驱动器快速获取.csv文件的最后一行,以便我不必仅加载整个巨大的.csv文件以使用最后一行?

1 个答案:

答案 0 :(得分:3)

假设您使用内置的reversed模块,有一个漂亮的csv工具:

how to read a csv file in reverse order in python

简而言之:

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

在我的测试文件中:

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

输出:

test, 3
test, 2
test, 1