问题
I have a algorithm that outputs a list of tuples which is ready to be written into a csv file.
I'm trying to write 3 csv files (through StringIO so no writing to disk) and then zip them altogether. After that I want to attach that to the response of a django request.
I'm not sure what's the most efficient way to do this. Should I use StringIO
to store the 3 calls through my algo? Should I actually create the csv files first before zipping them? Can I directly use 1 zipfile
call without the intermediate steps of calling 3 StringIO
s?
Thanks
回答1:
You can do something like:
# Create the zip file
output = StringIO.StringIO()
f = zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED)
f.writestr('first.csv', '<the content of first.csv>')
f.writestr('second.csv', '<the content of second.csv>')
f.writestr('third.csv', '<the content of third.csv>')
f.close()
# Build your response
response = HttpResponse(output.getvalue(), mimetype='application/zip')
response['Content-Disposition'] = 'attachment; filename="yourzipfilename.zip"'
return response
You may want to use a StreamingHttpResponse
(or FileResponse ) if the file is big https://stackoverflow.com/a/48949959/1904584
回答2:
Besides the answer posted by others, I was also able to solve my issue by
zipped_file = BytesIO()
with zipfile.ZipFile(zipped_file, 'a', zipfile.ZIP_DEFLATED) as zipped:
for h in HEADER: # determines which csv file to write
rs = build_my_csv(h)
csv_data = StringIO()
writer = csv.writer(csv_data, delimiter=',')
writer.writerow(HEADER[h])
for r in rs:
writer.writerow(r)
csv_data.seek(0)
zipped.writestr("{}.csv".format(h), csv_data.read())
zipped_file.seek(0)
response = HttpResponse(zipped_file, content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=some_name.zip'
This is using the idea from Sending multiple .CSV files to .ZIP without storing to disk in Python
来源:https://stackoverflow.com/questions/50952823/django-response-that-contains-a-zip-file-with-multiple-csv-files