Skip to content

Instantly share code, notes, and snippets.

@slavanap
Last active June 15, 2017 13:54
Show Gist options
  • Save slavanap/314022ab0d8406ea92564e9048dd8198 to your computer and use it in GitHub Desktop.
Save slavanap/314022ab0d8406ea92564e9048dd8198 to your computer and use it in GitHub Desktop.
Chunks to Lines in Python 2.7
def ChunksToLines(reader):
history = []
for chunk in reader:
lines = chunk.splitlines()
if chunk[-1] == '\n':
lines.append('')
elif len(lines) == 1:
history.append(lines[0])
continue
history.append(lines[0])
yield "".join(history)
for line in lines[1:-1]:
yield line
history = lines[-1:]
line = "".join(history)
if len(line) > 0:
yield line
# Test it with
for line in ChunksToLines(["abc\n", "\ncdb", "f\nzz", "\n", "avvvv", "b\nc"]): print line
for line in ChunksToLines(['\r\n1\r\n\r\n']): print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment