xin053
7/25/2018 - 12:33 AM

[python]读文件一次读n行

[python]读文件一次读n行

from itertools import islice


def next_n_lines(file_opened, N):
    return [x.strip() for x in islice(file_opened, N)]

  
with open("samplefile", 'r') as f:
    x = ""
    while x != []:
      x = next_n_lines(f, 5)  # returns a list
      pass  # do something to x