首页 > 解决方案 > 如何快速获取巨大 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')

标签: pythonpython-3.x

解决方案


这是我在 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 秒才得到最后一行。


推荐阅读