11👍
✅
open() takes the name of the file as the argument and not the file object itself.
Can you try something like this:
paramFile = request.FILES['uploadFile'].read()
portfolio = csv.DictReader(paramFile)
21👍
This works for Python 3
import csv
import io
...
csv_file = request.FILES['uploadFile']
decoded_file = csv_file.read().decode('utf-8')
io_string = io.StringIO(decoded_file)
for line in csv.reader(io_string, delimiter=';', quotechar='|'):
print(line)
- How to store django objects as session variables ( object is not JSON serializable)?
- How do I remove the square brackets at the end of a JS variable name during AJAX calls?
- Soft deleting objects in django
- Does a library to prevent duplicate form submissions exist for django?
13👍
No need to call open on the file, it’s already open. You should be able to pass it straight into the DictReader.
- Accessing form fields as properties in a django view
- Django nginx Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- Indirect inline in Django admin
10👍
Django 2, Python 3.6, same problem, half a day of googling, this is the solution that worked for me.
form.is_valid:
csv_file = request.FILES['file']
csv_file.seek(0)
reader = csv.DictReader(io.StringIO(csv_file.read().decode('utf-8')))
for row in reader
.....
.....
detailed writeup here -> source
- Add extra field to ModelForm
- How do I hide the field label for a HiddenInput widget in Django Admin?
- Problem launching docker-compose : python modules not installed
1👍
You get a TypeError, because the built in function open expects a string that is a path to a file.
Does this work?
if form.is_valid():
request.FILES['uploadFile'].open("rb")
portfolio = csv.DictReader(request.FILES['uploadFile'].file)
- How do you use Django-filter's '__in' lookup?
- Celerybeat not executing periodic tasks
- Django/Python Runtime Error: Maximum recursion depth exceeded
- Accessing django project in LAN systems
- Django admin add custom filter
Source:stackexchange.com