如何通过网络驱动器快速从.csv文件中获取最后一行? [英] How to quickly get the last line from a .csv file over a network drive?

查看:118
本文介绍了如何通过网络驱动器快速从.csv文件中获取最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

如何在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

这篇关于如何通过网络驱动器快速从.csv文件中获取最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆