pandas 如何通过网络驱动器从 .csv 文件中快速获取最后一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44681865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to quickly get the last line from a .csv file over a network drive?
提问by user1367204
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?
我.csv
在网络驱动器上的文件中存储了数千个时间序列。在更新文件之前,我首先获取文件的最后一行以查看时间戳,然后使用该时间戳之后的数据进行更新。如何.csv
通过网络驱动器快速获取文件的最后一行,这样我就不必加载整个大.csv
文件才能使用最后一行?
回答by Douglas
There is a nifty reversed
tool for this, assuming you are using the built-in csv
module:
reversed
假设您正在使用内置csv
模块,则有一个漂亮的工具:
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