简体   繁体   中英

How to downlaod a csv file after writing data to it using python

I am using web.py and creating a basic webpage,

I had a basic html code which has a button as below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home</title>
</head>
<body>
   <form method="POST" action="/retrieve">
      <button id="submit" name="submit">Retrieve</button>
   </form>
</body>

So by the above code i can able to see a button on a page, when we click on the button Retrieve the action attribute activates and goes to required path to perform operation

index.py code

import web
import csv

urls = (
  '/retrieve',   'Retrieve',
)

app = web.application(urls, globals())
conn = mdb.connect(user='root', passwd='redhat', db='Merion_dev', host='localhost')

class Retrieve:

    def POST(self):
        cursor = conn.cursor()
        query = "SELECT * FROM adm_facility LIMIT 0,10 "
        cursor.execute(query)
        result = cursor.fetchall() 
        csv_file = csv.writer(open('Test_File.csv', 'wb'))
        csv_file.writerow(['Facility_id', 'Name', 'Account Number', 'Street'])
        for i in result :
            csv_file.writerow([i[0],i[2],i[3],i[4]])
        raise web.seeother('/retrieve')

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run()  

So when i run the above code , a csv file is successfully created with data from database by the select query written.

Now what i want to do is, when we click on the retrieve button the data should be written in to csv file and should be downloaded like

In phpmyadmin when we click on export button a file will be downloaded in different formats according to our selection, so but here i wan t to download a file(csv) after saving data in to it.

Can anyone please let me know

  1. How can we download the csv file after saving data in to it by the above code

  2. How can we download the csv file using python in general ?

You don't have to write it to a file, you could use StringIO for it:

from StringIO import StringIO
import csv

import web

urls = (
  '/retrieve',   'Retrieve',
)

app = web.application(urls, globals())
conn = mdb.connect(user='root', passwd='redhat', db='Merion_dev', host='localhost')

class Retrieve:

    def POST(self):
        cursor = conn.cursor()
        query = "SELECT * FROM adm_facility LIMIT 0,10 "
        cursor.execute(query)
        result = cursor.fetchall() 
        csv_file = StringIO()
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow(['Facility_id', 'Name', 'Account Number', 'Street'])
        for i in result :
            csv_writer.writerow([i[0],i[2],i[3],i[4]])
        web.header('Content-Type','text/csv')
        web.header('Content-disposition', 'attachment; filename=yourfilename.csv')
        return csv_file.getvalue()

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM