Re: Flask – how to write csv file & save using prefilled value of the filename (response.headers["Content-Disposition"]="attachment; filename=xxx")

Roland Mueller roland.em0001 at googlemail.com
Mon Aug 9 13:01:52 EDT 2021


pe 6. elok. 2021 klo 19.15 MRAB (python at mrabarnett.plus.com) kirjoitti:

> On 2021-08-06 16:50, Suretha Weweje wrote:
> > I am trying to upload a CSV file with flask, read and process one line
> at a
> > time while iterating through all rows of the file and write the results
> > back to a new CSV file.  My python script produces the correct results on
> > its own, but I am not able to get the same results when using Flask. I am
> > new to flask.
> >
> [snip]
>  >
> >      csv_reader = csv.reader(new)
> >
> [snip]
> >
> >      *mywriter = csv.DictWriter('results.csv', fieldnames=headers_new)*
> [snip]
>
> The problem has nothing to do with Flask.
>
> 'csv.reader' and 'csv.DictWriter' expect a file object, but you're
> passing them strings.
> --
> https://mail.python.org/mailman/listinfo/python-list


May be you should use io.StringIO to create a file/stream from an input
string.

from io import StringIO

s = "\n".join(["a","b","c"]) + "\n"

for line in StringIO(s):
    print(f"\t{line}")

BR,
Roland


More information about the Python-list mailing list