generating a CSV file online on Google App Engine

2024/9/16 23:07:39

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..

Answer

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

https://en.xdnf.cn/q/72564.html

Related Q&A

Python equivalence of Rs match() for indexing

So i essentially want to implement the equivalent of Rs match() function in Python, using Pandas dataframes - without using a for-loop. In R match() returns a vector of the positions of (first) matches…

Why doesnt Pydantic validate field assignments?

I want to use Pydantic to validate fields in my object, but it seems like validation only happens when I create an instance, but not when I later modify fields. from pydantic import BaseModel, validato…

Format OCR text annotation from Cloud Vision API in Python

I am using the Google Cloud Vision API for Python on a small program Im using. The function is working and I get the OCR results, but I need to format these before being able to work with them.This is …

Does pybtex support accent/special characters in .bib file?

from pybtex.database.input import bibtex parser = bibtex.Parser() bibdata = parser.parse_file("sample.bib")The above code snippet works really well in parsing a .bib file but it seems not to …

How do I count specific values across multiple columns in pandas

I have the DataFrame df = pd.DataFrame({colA:[?,2,3,4,?],colB:[1,2,?,3,4],colC:[?,2,3,4,5] })I would like to get the count the the number of ? in each column and return the following output - colA…

Split Python source into separate directories?

Here are some various Python packages my company "foo.com" uses:com.foo.bar.web com.foo.bar.lib com.foo.zig.web com.foo.zig.lib com.foo.zig.lib.lib1 com.foo.zig.lib.lib2Heres the traditional …

How can I use a raw_input with twisted?

I am aware that raw_input cannot be used in twisted. However here is my desired application.I have an piece of hardware that provides an interactive terminal serial port. I am trying to connect to th…

How to use Python and HTML to build a desktop software?

Maybe my question is stupid but I still want to ask. I am always wondering whether I can use Python, HTML and Css to develop a desktop software. I know there are alrealy several good GUI frameworks lik…

More efficient way to look up dictionary values whose keys start with same prefix

I have a dictionary whose keys come in sets that share the same prefix, like this:d = { "key1":"valA", "key123":"valB", "key1XY":"valC","…

When should I use dt.column vs dt[column] pandas?

I was doing some calculations and row manipulations and realised that for some tasks such as mathematical operations they both worked e.g.d[c3] = d.c1 / d. c2 d[c3] = d[c1] / d[c2]I was wondering wheth…