Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django csv file upload managing

def createlist(request):
    if request.method == 'POST':
        files =  request.FILES['ListFile']
        print(type(files))
        csv_file = csv.DictReader(files)
        for i in csv_file:
            print(i)
    return HttpResponse("ok")

This gives
class 'django.core.files.uploadedfile.InMemoryUploadedFile'
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Here i post this file using ajax
js:

$('#form1').ajaxForm(function(data) { 
           alert(data) ; 
        });
like image 327
itzMEonTV Avatar asked May 29 '14 07:05

itzMEonTV


1 Answers

Using codec.iterdecode, i solved it.I think this is due to python 3.x

import codecs

def createlist(request):
    if request.method == "POST":    
        fil =  request.FILES['ListFile']
        csvfile = csv.DictReader(codecs.iterdecode(fil, 'utf-8'))
        for i in csv_file:
            print(i)
    return HttpResponse("ok") 
like image 170
itzMEonTV Avatar answered Nov 16 '22 04:11

itzMEonTV