Last active
June 15, 2017 13:54
-
-
Save slavanap/314022ab0d8406ea92564e9048dd8198 to your computer and use it in GitHub Desktop.
Chunks to Lines in Python 2.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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