python - 如何快速获取巨大 csv 文件的最后一行(48M 行)?
问题描述
我有一个 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 秒才得到最后一行。
推荐阅读
- django - Django - 显示用户列表需要超过 14 秒,如何加快速度?
- android - 在 NativeScript 上为 Android 设置安全插入
- c++ - C ++ Winsock2客户端未通过远程IP连接到服务器
- php - Windows 中的 Xampp PHP7 和 PHP5
- javascript - Vue 在 10 秒后导航到下一条路线
- html - 重新提交表单后,如何用用户的评论重新填充文本区域?(使用 HTML 和 PHP)
- unity3d - 如何统一获得从白日梦控制器到指向游戏对象的距离?
- swift - NSTextView 仅在当前滚动到底部时才滚动到底部?
- c++ - C++ 中的文件处理:更新一行
- python - 如何为 python cartopy 几何设置偏移量?