Export a .csv file using web.py – (python:))
December 22nd, 2009 Posted in Scripts for Testing
This script is for Exporting a csv file and get the browser to recognize that its a csv file and popups the download window with web.py. Lets say we have a database with a table called users and you want to create a csv file that contains all the users with their names and id’s here is how you do it.
class export:
def GET(self):
i = web.input()
users = web.select(’users‘, vars=locals())
csv = []
csv.append(”id,name\n“)
for user in users:
row = []
row.append(user.id)
row.append(user.name)
csv.append(”,“.join(row))
#writer.writerow(row)
#f.close()
web.header(’Content-Type‘,’text/csv‘)
web.header(’Content-disposition‘, ‘attachment; filename=export.csv‘)
print “”.join(csv)
return


