Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ease the RAM usage when exporting big tables to CSV #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions commcare_export/writers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csv
import datetime
import io
from tempfile import NamedTemporaryFile
import zipfile
from itertools import zip_longest

Expand Down Expand Up @@ -86,20 +86,20 @@ def __enter__(self):

def write_table(self, table):
if self.archive is None:
raise Exception('Attempt to write to a closed CsvWriter')

tempfile = io.StringIO()
writer = csv.writer(tempfile, dialect=csv.excel)
writer.writerow(table.headings)
for row in table.rows:
writer.writerow(row)

# TODO: make this a polite zip and put everything in a subfolder
# with the same basename as the zipfile
self.archive.writestr(
'%s.csv' % self.zip_safe_name(table.name),
tempfile.getvalue().encode('utf-8')
)
raise RuntimeError('Attempt to write to a closed CsvWriter')

with NamedTemporaryFile(mode='w') as tempfile:
writer = csv.writer(tempfile, dialect=csv.excel)
writer.writerow(table.headings)
for row in table.rows:
writer.writerow(row)

# TODO: make this a polite zip and put everything in a subfolder
# with the same basename as the zipfile
self.archive.write(
tempfile.name,
'%s.csv' % self.zip_safe_name(table.name),
)

def __exit__(self, exc_type, exc_val, exc_tb):
self.archive.close()
Expand Down