分享

[Python]如何取出一个超大文本文件的最后几行

 java_laq小馆 2015-01-19

[Python]如何取出一个超大文本文件的最后几行

分类: Python 2926人阅读 评论(3) 收藏 举报
  这是 python-中国 邮件列表中的一个问题。如果不考虑效率问题,这个问题可能很简单,直接read出整个文件的内容,然后split一下或者 还有更简单的办法。但是如果这个文件是“超大”,那么为了区区几行而读入整个文件有点‘“得不偿失“。下面是我的实现。刚学python不久,也许没有发 挥出它的优点。

#last lines
#by Kevin Yuan
def last_lines(filename, lines = 1):
    #print the last line(s) of a text file
    """
    Argument filename is the name of the file to print.
    Argument lines is the number of lines to print from last.
    """
    block_size = 1024
    block = ''
    nl_count = 0
    start = 0
    fsock = file(filename, 'rU')
    try:
        #seek to end
        fsock.seek(0, 2)
        #get seek position
        curpos = fsock.tell()
        while(curpos > 0): #while not BOF
            #seek ahead block_size+the length of last read block
            curpos -= (block_size + len(block));
            if curpos < 0: curpos = 0
            fsock.seek(curpos)
            #read to end
            block = fsock.read()
            nl_count = block.count('/n')
            #if read enough(more)
            if nl_count >= lines: break
        #get the exact start position
        for n in range(nl_count-lines+1):
            start = block.find('/n', start)+1
    finally:       
        fsock.close()
    #print it out 
    print block[start:]
   
if __name__ == '__main__':
    import sys
    last_lines(sys.argv[0], 5) #print the last 5 lines of THIS file

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多