我有多个数据帧,我想使用flask返回多个可下载的CSV文件。
我尝试了以下代码,但它只能返回一个可下载的 CSV 文件。
resp = make_response(df.to_csv())
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp
我尝试返回多个响应 (return resp1, resp2
) 但出现以下错误:
TypeError: %d format: a number is required, not Response
任何帮助将不胜感激,谢谢!
答案 0 :(得分:1)
我得到了答案,实际上无法仅使用flask一次返回所有文件,因此我制作了一个包含所有CSV的zip文件,然后将其返回。
import zipfile
with zipfile.ZipFile('main_code/output.zip', 'w') as csv_zip:
csv_zip.writestr("data1.csv", data1.to_csv())
csv_zip.writestr("data2.csv", data2.to_csv())
return send_file('output.zip',
mimetype='zip',
attachment_filename='output.zip',
as_attachment=True)