4

I'm trying to create pandas dataframe from uploaded csv file without saving file. When I do df = pd.read_csv(request.file['file']) pandas read this as a file but EmptyDataError: No columns to parse from file is shown. But this file is loaded properly to dataframe from console. request.file['file'].stream doesn't work also.

Dmitry Sazhnev
  • 383
  • 3
  • 16
  • I had the same problem but, it was solved by using FileStorage.stream. It points to a temporary copy of the file. See the documentation here: https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage – Akalanka Weerasooriya May 13 '20 at 11:57

2 Answers2

2

If you are receiving your csv as a string try it using StringIO:

from io import StringIO
pd.read_csv(StringIO(request.file['file']))
zipa
  • 27,316
  • 6
  • 40
  • 58
0

I'm having the same issue here but maybe this could help you in your research. so when you upload a file and handle it with flask you get a Werkzeug ~werkzeug.datastructures.FileStorage object, when you do that without putting this attribute : enctype="multipart/form-data" in the html form if it's not specified the file will be empty.

https://flask.palletsprojects.com/en/2.2.x/patterns/fileuploads/

Maybe you already know this, I hope it helps anyone else that finds this

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 11 '22 at 13:47