Skip to content

Instantly share code, notes, and snippets.

@cypreess
Created October 30, 2018 10:29
Show Gist options
  • Save cypreess/ea711263a9b40e2932b33b576128aa1f to your computer and use it in GitHub Desktop.
Save cypreess/ea711263a9b40e2932b33b576128aa1f to your computer and use it in GitHub Desktop.
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