DirtyHands
DirtyHands

Reputation: 149

how to load csv file data into pandas using request.FILES(django 1.11) without saving file on server

i just want to upload .csv file via form, directly in to pandas dataframe in django without saving physically file on to server.

def post(self, request, format=None):
        try:
            from io import StringIO, BytesIO
            import io
            print("data===",request.FILES['file'].read().decode("utf-8"))
            # print("file upload FILES data=====",pd.read_csv(request.FILES['file'].read(), sep=','))
            #print(request.FILES)
            print("file upload data df=====11")
            mm = pd.read_csv( BytesIO(request.FILES['file'].read().decode("utf-8")))
            print("dataframe  data=====",mm)
            # import io, csv
            # urlData = request.FILES['file']
            # data = [row for row in (csv.reader(urlData))]
            # print("file upload data df=====222",data)

            # mm = pd.read_csv()

            #excel_file = request.FILES['file']
            # movies = pd.read_excel(request.FILES['file'])
    except Exception as e:
        print(e)
        log.debug("Error in CheckThreadStatus api key required "+str(e))
        return Response(responsejson('api key required', status=404))

Upvotes: 0

Views: 4073

Answers (3)

primegxy
primegxy

Reputation: 1858

You can use StringIO for reading and decoding your csv :

import csv
from io import StringIO


csv_file = request.FILES["csv_file"]
content = StringIO(csv_file.read().decode('utf-8'))
reader = csv.reader(content)

After reading you can populate your database like this :

csv_rows = [row for row in reader]
field_names = csv_rows[0]  # Get the header row
del csv_rows[0] # Deleting header after storing it's values in field_names

for index, row in enumerate(csv_rows):
   data_dict = dict(zip(field_names, row))
   Model.objects.update_or_create(id=row[0],
     defaults=data_dict
   )

Make sure to validate data before inserting, if the data is critical.

HINT: use django forms to validate for you.
from django import forms

Upvotes: 0

Anil Yadav
Anil Yadav

Reputation: 251

Check With pd.read_csv('data.csv') # doctest: +SKIP

If using post method you can try getFile = request.FILE['file_name']

pd.read_csv(getFile) # doctest: +SKIP

Upvotes: 0

DirtyHands
DirtyHands

Reputation: 149

the ans is straight forward: that is

    pd.read_csv(request.FILES['file'])

works perfectly fine, the mistake i was doing is that.. my csv file was not in correct format.

Upvotes: 2

Related Questions