Django response that contains a zip file with multiple csv files

本秂侑毒 提交于 2019-12-21 22:55:28

问题


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 StringIOs?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!