6
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) ; 
        });
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49

2 Answers2

14

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") 
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

The CSV doesn't support UTF8. It needs the file-like object to be encoded. Refer to here for more information.

import codecs

def createlist(request):
    if request.method == "POST":
        utf8_file = codecs.EncodedFile(request.FILES["ListFile"],"utf-8")
        csv_file = csv.DictReader(utf8_file)
        for i in csv_file:
            print(i)
    return HttpResponse("ok")
Community
  • 1
  • 1
dilbert
  • 3,008
  • 1
  • 25
  • 34