Skip to content

Instantly share code, notes, and snippets.

@Cediddi
Created November 26, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cediddi/d202b94b8550176eaddeee19a4df3490 to your computer and use it in GitHub Desktop.
Save Cediddi/d202b94b8550176eaddeee19a4df3490 to your computer and use it in GitHub Desktop.
A very simple read n lines implementation, available in all natural, classic and generator flavors.
def readnlines(filehandler, n=4):
line_groups = []
while True:
nlines = []
for i in range(n):
line = filehandler.readline()
if line:
nlines.append(line.strip())
else:
if nlines:
line_groups.append(nlines)
return line_groups
else:
line_groups.append(nlines)
def xreadnlines(filehandler, n=4):
line_cache = []
for line in filehandler:
line_cache.append(line.strip())
if len(line_cache) == n:
yield line_cache
line_cache = []
else:
yield line_cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment