generating a CSV file online on Google App Engine

2019-03-21 05:01发布

I am using Google App Engine (python), I want my users to be able to download a CSV file generated using some data from the datastore (but I don't want them to download the whole thing, as I re-order the columns and stuff).

I have to use the csv module, because there can be cells containing commas. But the problem that if I do that I will need to write a file, which is not allowed on Google App Engine

What I currently have is something like this:

    tmp = open("tmp.csv", 'w')
    writer = csv.writer(tmp)
    writer.writerow(["foo", "foo,bar", "bar"])

So I guess what I would want to do is either to handle cells with commas.. or to use the csv module without writing a file as this is not possible with GAE..

3条回答
虎瘦雄心在
2楼-- · 2019-03-21 05:16

I found a way to use the CSV module on GAE! Here it is:

self.response.headers['Content-Type'] = 'application/csv'
writer = csv.writer(self.response.out)

writer.writerow(["foo", "foo,bar", "bar"])

This way you don't need to write any files

查看更多
家丑人穷心不美
3楼-- · 2019-03-21 05:16

Here is a complete example of using the Python CSV module in GAE. I typically use it for creating a csv file from a gql query and prompting the user to save or open it.

import csv

class MyDownloadHandler(webapp2.RequestHandler):
  def get(self):

    q = ModelName.gql("WHERE foo = 'bar' ORDER BY date ASC")
    reqs = q.fetch(1000)

    self.response.headers['Content-Type'] = 'text/csv'
    self.response.headers['Content-Disposition'] = 'attachment; filename=studenttransreqs.csv'
    writer = csv.writer(self.response.out)

create row labels

    writer.writerow(['Date', 'Time','User' ])

iterate through query returning each instance as a row

    for req in reqs:
        writer.writerow([req.date,req.time,req.user])

Add the appropriate mapping so that when a link is clicked, the file dialog opens

('/mydownloadhandler',MyDownloadHandler),
查看更多
倾城 Initia
4楼-- · 2019-03-21 05:25
import StringIO

tmp = StringIO.StringIO()
writer = csv.writer(tmp)

writer.writerow(["foo", "foo,bar", "bar"])
contents = tmp.getvalue()

tmp.close()
print contents
查看更多
登录 后发表回答