Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload CSV file using Python Flask and process it

I have the following code to upload an CSV file using Python FLASK.

from flask_restful import Resource
import pandas as pd

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

class UploadCSV(Resource):

    def post(self):
        files = request.files['file']
        files.save(os.path.join(ROOT_PATH,files.filename))
        data = pd.read_csv(os.path.join(ROOT_PATH,files.filename))
        print(data)

api.add_resource(UploadCSV, '/v1/upload')

if __name__ == '__main__':
    app.run(host='localhost', debug=True, port=5000)

This code works fine and I can able to upload CSV file successfully and read it using pandas dataframe. But I'm saving the csv in local filesystem and reading it.

I have tried reading like below -

files = request.files['file']
files.read()

The results obtained were in the format of bytes but I need it in the format of dictionary. So I used pandas dataframe to read it and I later convert it into a custom dictionary of my format.

Is it possible to read the CSV file on the fly without writing it in local filesystem? Or any equivalent way we can achieve using Python Flask Restful?

like image 328
Dinesh Avatar asked Mar 20 '19 04:03

Dinesh


1 Answers

The (not so aptly named, due to it being one file) files variable contains a werkzeug.datastructures.FileStorage object. This is a file like object (containing a read() method), and as such it is possible to use it directly as input to pandas.read_csv()as seen in the pandas documentation.

Thus, the solution to not save to disk is as simple as:

class UploadCSV(Resource):

    def post(self):
        file = request.files['file']
        data = pd.read_csv(file)
        print(data)
like image 175
JohanL Avatar answered Sep 24 '22 13:09

JohanL