Python Django响应,包含一个包含多个csv文件的zip文件
我有一个算法,输出一个元组列表,可以写入csv文件 我正在尝试写3个csv文件(通过StringIO,这样就不会写入磁盘),然后将它们全部压缩。之后,我想将其附加到django请求的响应中 我不确定做这件事最有效的方法是什么。我应该使用Python Django响应,包含一个包含多个csv文件的zip文件,python,django,zipfile,stringio,Python,Django,Zipfile,Stringio,我有一个算法,输出一个元组列表,可以写入csv文件 我正在尝试写3个csv文件(通过StringIO,这样就不会写入磁盘),然后将它们全部压缩。之后,我想将其附加到django请求的响应中 我不确定做这件事最有效的方法是什么。我应该使用StringIO通过我的algo存储这3个调用吗?我应该在压缩csv文件之前先创建它们吗?我可以直接使用1zipfilecall而不调用3StringIOs中间步骤吗 谢谢您可以执行以下操作: # Create the zip file output = Stri
StringIO
通过我的algo存储这3个调用吗?我应该在压缩csv文件之前先创建它们吗?我可以直接使用1zipfile
call而不调用3StringIO
s中间步骤吗
谢谢您可以执行以下操作:
# 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
#创建zip文件
输出=StringIO.StringIO()
f=zipfile.zipfile(输出“w”,zipfile.ZIP_已放气)
f、 writestr('first.csv','')
f、 writestr('second.csv','')
f、 writestr('third.csv','')
f、 关闭()
#建立你的回应
response=HttpResponse(output.getvalue(),mimetype='application/zip')
响应['Content-Disposition']='附件;filename=“yourzipfilename.zip”'
返回响应
您可能希望使用
流化TTPRESSONSE
(或者)如果文件很大,您可以执行以下操作:
# 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
#创建zip文件
输出=StringIO.StringIO()
f=zipfile.zipfile(输出“w”,zipfile.ZIP_已放气)
f、 writestr('first.csv','')
f、 writestr('second.csv','')
f、 writestr('third.csv','')
f、 关闭()
#建立你的回应
response=HttpResponse(output.getvalue(),mimetype='application/zip')
响应['Content-Disposition']='附件;filename=“yourzipfilename.zip”'
返回响应
您可能需要使用
StreamingHttpResponse
(或者)如果文件很大除了其他人发布的答案之外,我还可以通过
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'
这是利用中的想法,除了其他人发布的答案外,我还能够通过
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'
这是使用来自的想法,谢谢。我用了类似的方法来解决这个问题。我会把你的答案标为正确答案。谢谢。我用了类似的方法来解决这个问题。我将把你的答案标记为正确答案。
标题
只是一个字符串列表,如['a','b','c']
标题
只是一个字符串列表,如['a','b','c']