Skip to content

Instantly share code, notes, and snippets.

@finomayato
Created June 25, 2018 17:39
Show Gist options
  • Select an option

  • Save finomayato/ce51552f7ade47ff6699972b4c823777 to your computer and use it in GitHub Desktop.

Select an option

Save finomayato/ce51552f7ade47ff6699972b4c823777 to your computer and use it in GitHub Desktop.
import tempfile
class ChunkWriter:
pass
class CSVChunkWriter:
def __init__(self, filename=None):
self.filename = filename
if filename:
self.file_ = open(filename, 'wb')
else:
self.file_ = tempfile.NamedTemporaryFile()
def write_chunk(self, df):
if self.first_write:
df.to_csv(self.file_, mode='a', encoding='utf-8')
else:
df.to_csv(self.file_, mode='a', encoding='utf-8', header=False)
def finish(self):
pass
class JSONChunkWriter:
def __init__(self, filename=None):
self.filename = filename
if filename:
self.file_ = open(filename, 'w')
else:
self.file_ = tempfile.NamedTemporaryFile()
self.file_.write('[')
def write_chunk(self, json_string):
"""store_chunk
:param json_string: output from `json.dumps` function
:type json_string: str
"""
json_string_formatted = ', {}'.format(json_string[:-1]) # last character is ']'
self.file_.write(json_string_formatted)
def finish(self):
self.file_.write(']')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment