1

I have been facing an issue with file upload , so i will be uploading a file on submit , i want to collect the file read the data in it and add it to the database , I am constantly getting this error

Traceback (most recent call last):
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/vinaykashyap/Desktop/Deploy-Testing2/annotating/views.py", line 244, in UploadAdmin
    next(reader)  # skips header
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/csv.py", line 111, in __next__
    self.fieldnames
  File "/Users/vinaykashyap/opt/anaconda3/lib/python3.7/csv.py", line 98, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

It will be helpful anyone of you can suggest , how it needs to done . Thanks in Advance

Views.py

def UploadAdmin(request):
    if request.method == 'POST':
         if request.POST.get("action_name") == 'NEXT':
                #  form =  request.FILES['my_uploaded_file'].read()
                 reader = csv.DictReader(request.FILES['my_uploaded_file'].file)
                 next(reader)  # skips header
                 for row in reader:   
                     _, created = NewsItem.objects.get_or_create(
                     headline=row[0],
                     content=row[1],
                     collection=row[2],
                     url=row[3],
                     chunk_id=row[4]
                  )
    return render(request,'annotating/uploadDataAdmin.html')

1 Answer 1

4

at the risk of a bit much magic, I'd do something like:

import codecs

column_names = ['headline', 'content', 'collection', 'url', 'chunk_id']

def form_handler(request):
    if request.method == 'POST':
        file = request.FILES['my_uploaded_file']

        # iteratively decode file from bytes to string and interpret as CSV
        reader = csv.reader(codecs.iterdecode(file, 'utf-8'))

        # skip the header
        next(reader)

        # create items from remaining rows
        for row in reader:
            NewsItem.objects.get_or_create(
                **dict(zip(column_names, row))
            )

the important fact is that the file is binary and csv.reader expects strings, codecs.iterdecode is a nice way to do that. if you are sure you have a small amount of data you could just read it all in then decode the bytes into a string. for example you might use:

  reader = csv.reader(file.read().decode('utf8').splitlines())

instead, but I'd suggest using iterdecode. the column_names stuff is just a shorthand way of doing what your code was doing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.