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.
Asked
Active
Viewed 5,130 times
4

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 Answers
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
-
1
-
4Call `read` method on `FileStorage` object. See - https://stackoverflow.com/a/20017830/2732017 – userx Jun 04 '19 at 11:15
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

Omar Benaidy
- 1
- 1
-
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