Created
October 30, 2018 10:29
-
-
Save cypreess/ea711263a9b40e2932b33b576128aa1f to your computer and use it in GitHub Desktop.
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
import re | |
def zstd_readline(filename, chunksize=zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE): | |
with open(filename, 'rb') as fh: | |
with zstd.ZstdDecompressor().stream_reader(fh) as reader: | |
leftovers = '' | |
while True: | |
chunk = reader.read(chunksize).decode('utf-8') | |
if not chunk: | |
break | |
start_position = 0 | |
for o in re.finditer('\n', chunk): | |
if leftovers: | |
yield leftovers + chunk[start_position:o.start()] | |
leftovers = '' | |
else: | |
yield chunk[start_position:o.start()] | |
start_position = o.end() | |
leftovers = chunk[start_position:] | |
if leftovers: | |
yield leftovers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment