Home > Mobile >  Iterator should return strings, not bytes (the file should be opened in text mode)
Iterator should return strings, not bytes (the file should be opened in text mode)

Time:01-21

this is my code..

def import_excel(request):

  if request.method == 'POST':
    person_resource = PersonResource()

    dataset  = Dataset()
    new_person = request.FILES['myfile']
    if not new_person.name.endswith('csv'):
      messages.info(request,'Wrong format')
      return render(request,'upload.html')
    
    imported_data = dataset.load(new_person.read(),format='csv')
    for data in imported_data:
      value = Person(
        data[0],
        data[1],
        data[2]
      )
     value.save()
  return render(request,'upload.html')

while importing the csv file to the database getting the error:

iterator should return strings, not bytes (the file should be opened in text mode)

like this

CodePudding user response:

You can read the data of the uploaded file as follows and the problem will be solved.

new_person = request.FILES['myfile'].read().decode("utf-8")
  • Related