gpt4 book ai didi

python - 如何快速获取一个巨大的 csv 文件的最后一行(48M 行)?

转载 作者:行者123 更新时间:2023-12-04 11:14:44 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to read the last line of a file in Python?

(7 个回答)


6 个月前关闭。




我有一个 csv 文件,它会一直增长到大约 48M 行。
在向它添加新行之前,我需要阅读最后一行。
我尝试了下面的代码,但它太慢了,我需要一个更快的替代方案:

def return_last_line(filepath):    
with open(filepath,'r') as file:
for x in file:
pass
return x
return_last_line('lala.csv')

最佳答案

这是我的看法,在 python 中:
我创建了一个函数,让您选择最后几行,因为最后几行可能是空的。

def get_last_line(file, how_many_last_lines = 1):

# open your file using with: safety first, kids!
with open(file, 'r') as file:

# find the position of the end of the file: end of the file stream
end_of_file = file.seek(0,2)

# set your stream at the end: seek the final position of the file
file.seek(end_of_file)

# trace back each character of your file in a loop
n = 0
for num in range(end_of_file+1):
file.seek(end_of_file - num)

# save the last characters of your file as a string: last_line
last_line = file.read()

# count how many '\n' you have in your string:
# if you have 1, you are in the last line; if you have 2, you have the two last lines
if last_line.count('\n') == how_many_last_lines:
return last_line
get_last_line('lala.csv', 2)
这个 lala.csv 有 4800 万行,比如在你的例子中。我花了 0 秒才拿到最后一行。

关于python - 如何快速获取一个巨大的 csv 文件的最后一行(48M 行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66507206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com