I'm developing an application in python with django. User can upload a CSV file. I use file upload to get the file. But, it's not stored any where. I try to take it from request to process the file. While I'm trying to open the file, it gives an error. I use the CSV library exists in python to process. Form elements and attributes used as per django. Request object which I try to take the uploaded file is also django designed object.
import csv
from rootFolder.UploadFileForm
def uploadFile(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
paramFile = open(request.FILES['uploadFile'], 'rb')
portfolio = csv.DictReader(paramFile)
users = []
for row in portfolio:
users.append(row)
This below line gives the error.
paramFile = open(request.FILES['uploadFile'], 'rb')
The given error is :
TypeError: coercing to Unicode: need string or buffer, InMemoryUploadedFile found
Please kindly give your suggestion if you got any idea on this. Thanks in advance.
No need to call open on the file, it's already open. You should be able to pass it straight into the DictReader.
You get a TypeError, because the built in function open expects a string that is a path to a file.
Does this work?
Django 2, Python 3.6, same problem, half a day of googling, this is the solution that worked for me.
detailed writeup here -> source
This works for Python 3
open() takes the name of the file as the argument and not the file object itself.
Can you try something like this: