Python how to read N number of lines at a time

后端 未结 6 1079
感情败类
感情败类 2020-11-27 03:42

I am writing a code to take an enormous textfile (several GB) N lines at a time, process that batch, and move onto the next N lines until I have completed the entire file.

6条回答
  •  醉梦人生
    2020-11-27 03:50

    Used chunker function from What is the most “pythonic” way to iterate over a list in chunks?:

    from itertools import izip_longest
    
    def grouper(iterable, n, fillvalue=None):
        "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
        args = [iter(iterable)] * n
        return izip_longest(*args, fillvalue=fillvalue)
    
    
    with open(filename) as f:
        for lines in grouper(f, chunk_size, ""): #for every chunk_sized chunk
            """process lines like 
            lines[0], lines[1] , ... , lines[chunk_size-1]"""
    

提交回复
热议问题