Skip to content

Instantly share code, notes, and snippets.

@xin053
Forked from y1zhou/read_n_lines.py
Last active July 4, 2019 07:47
Show Gist options
  • Save xin053/6e82b40caf336d9ac610af7d600a1b25 to your computer and use it in GitHub Desktop.
Save xin053/6e82b40caf336d9ac610af7d600a1b25 to your computer and use it in GitHub Desktop.
[python 读取文件]读文件一次读n行 #python #文件
with open('test.txt') as f
for line in f:
print(line)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment