Python how to read N number of lines at a time

后端 未结 6 1076
感情败类
感情败类 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 04:05

    islice() can be used to get the next n items of an iterator. Thus, list(islice(f, n)) will return a list of the next n lines of the file f. Using this inside a loop will give you the file in chunks of n lines. At the end of the file, the list might be shorter, and finally the call will return an empty list.

    from itertools import islice
    with open(...) as f:
        while True:
            next_n_lines = list(islice(f, n))
            if not next_n_lines:
                break
            # process next_n_lines
    

    An alternative is to use the grouper pattern:

    with open(...) as f:
        for next_n_lines in izip_longest(*[f] * n):
            # process next_n_lines
    

提交回复
热议问题