I am trying to upload a CSV file, work on it to produce results, and write back (download) a new CSV file containing the result. I am very new to Flask and I am not able to
Important note: This answer is relevant only for platforms where SpooledTemporaryFile is available.
Further to iLuveTux answer, you can save the redundant read()
call by replacing the following string-based stream creation:
stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None)
with:
stream = io.TextIOWrapper(f.stream._file, "UTF8", newline=None)
Example:
stream = io.TextIOWrapper(f.stream._file, "UTF8", newline=None)
csv_input = csv.reader(stream)
print(csv_input)
for row in csv_input:
print(row)
Further information:
Werkzeug default stream for form data parser is SpooledTemporaryFile (as of 1.0.1), from which you can obtain the underlying buffer using its _file
memeber.