从Flask框架写一个CSV [英] Writing a CSV from Flask framework
问题描述
以下是我所关注的模板
@app。 $($ / $ download)
def download():
csv =REVIEW_DATE,AUTHOR,ISBN,DISCOUNTED_PRICE
1985/01/21 ,Douglas Adams,0345391802,5.95
1990/01/12,Douglas Hofstadter,0465026567,9.95
1998/07/15,TimothyThe Parser Campbell,0968411304,18.99
1999/12/03,Richard Friedman,0060630353,5.95
2004/10/04,Randel Helms,0879755725,4.50
response = make_response(csv)
response.headers [Content-Disposition] =attachment; filename = books.csv
return response
完美的写入了CSV,但是当我用我的代码尝试时,我得到了一长串。
我的代码:
@ app.route('/ download ')
def post(self):
#很多代码
csvList.append([all,my,data,goes,here])
csvList = str(re.sub('\ [| \]','',str(csvList)))#转换为字符串;删除括号
response = make_response(csvList)
response.headers ['Content-Disposition'] =attachment; filename = myCSV.csv
return response
我的输出:
<$ c $美国田纳西州古德利茨维尔La Petite学院下午老师助理(615)859-2034,Nashville Physician Service Ce,美国田纳西州布伦特伍德财政专员,(615)507-1646,Nashville Physician Service Ce,Denial Resolution Specialist,Brentwood ,TN,(615)507-1646
谢谢。
编辑:我尝试了所有的答案,他们大部分工作,但我选择了vectorfrog的,因为它符合我想要完成的。
最近我做了类似的事情,我发现我需要先将csv放入StringIO中,然后返回StringIO。如果您希望csv是一个下载,这是我做的:
import StringIO
import csv
from flask import make_response
@ app.route('/ download')
def post(self):
si = StringIO.StringIO()
cw = csv.writer(si)
cw.writerows(csvList)
output = make_response(si.getvalue())
output.headers [Content-Disposition] =attachment; filename = export.csv
output.headers [Content-type] =text / csv
返回输出
I have no problems writing a CSV outside of the Flask framework. But when I try to write it from Flask, it writes to the CSV, but only on one line.
Here is the template I'm following
@app.route('/download')
def download():
csv = """"REVIEW_DATE","AUTHOR","ISBN","DISCOUNTED_PRICE"
"1985/01/21","Douglas Adams",0345391802,5.95
"1990/01/12","Douglas Hofstadter",0465026567,9.95
"1998/07/15","Timothy ""The Parser"" Campbell",0968411304,18.99
"1999/12/03","Richard Friedman",0060630353,5.95
"2004/10/04","Randel Helms",0879755725,4.50"""
response = make_response(csv)
response.headers["Content-Disposition"] = "attachment; filename=books.csv"
return response
This writes the CSV perfectly, but when I try with my code, I get one long row.
My code:
@app.route('/download')
def post(self):
# lots of code
csvList.append([all,my,data,goes,here])
csvList = str(re.sub('\[|\]','',str(csvList))) # convert to a string; remove brackets
response = make_response(csvList)
response.headers['Content-Disposition'] = "attachment; filename=myCSV.csv"
return response
My output:
Nashville Physician Service Ce,Treasury Specialist,Brentwood,TN,(615) 507-1646,La Petite Academy,Afternoon Teacher Aide,Goodlettsville,TN,(615) 859-2034,Nashville Physician Service Ce,Denial Resolution Specialist,Brentwood,TN,(615) 507-1646
Thanks.
EDIT: I tried just about all the answers and they worked for the most part, but I chose vectorfrog's because it fit with what I was trying to accomplish.
I did something like this recently, I found that I needed to first place the csv into a StringIO and then return the StringIO. If you want the csv to be a download, here's what I did:
import StringIO
import csv
from flask import make_response
@app.route('/download')
def post(self):
si = StringIO.StringIO()
cw = csv.writer(si)
cw.writerows(csvList)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=export.csv"
output.headers["Content-type"] = "text/csv"
return output
这篇关于从Flask框架写一个CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!