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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:51:04  来源:igfitidea点击:

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

pythoncsvpandasnetwork-programmingpython-os

提问by user1367204

I store thousands of time series in .csvfiles 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 .csvfile over a network drive so that I don't have to load the entire huge .csvfile only to use the last line?

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

回答by Douglas

There is a nifty reversedtool for this, assuming you are using the built-in csvmodule:

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

how to read a csv file in reverse order in python

如何在python中以相反的顺序读取csv文件

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