我正在为我的Django网站开发一个报告应用程序。我想运行多个报告,并且每个报告都可以在内存中生成一个.csv文件,以.zip格式批量下载。我希望能够在不将任何文件存储到磁盘的情况下实现这一点。到目前为止,为了生成单个.csv文件,我正在执行常见的操作:
mem_file = StringIO.StringIO()
writer = csv.writer(mem_file)
writer.writerow(["My content", my_value])
mem_file.seek(0)
response = HttpResponse(mem_file, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=my_file.csv'
这个方法很好用,但是只适用于单一、未压缩的 .csv 文件。如果我有例如使用StringIO流创建的 .csv 文件列表:
firstFile = StringIO.StringIO()
# write some data to the file
secondFile = StringIO.StringIO()
# write some data to the file
thirdFile = StringIO.StringIO()
# write some data to the file
myFiles = [firstFile, secondFile, thirdFile]
我该如何返回一个压缩文件,其中包含 myFiles
中的所有对象,并可以正确地解压缩以显示三个 .csv 文件?