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.