0

I am simply trying to read a csv file in django rest framework but its giving me this error and I can't seem to find what is the issue because I have also tried reading the file in 'r' mode.

Error

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

Views.py (csv reading code)

 csv_file = request.FILES["Bus"]
        data = []
        with csv_file.open('r') as file:
            reader = csv.DictReader(file)
            for r in reader:
                print(r)

Full Traceback

Traceback (most recent call last):
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\django\core\handlers\exception.py", line 56, in inner
    response = get_response(request)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\django\views\decorators\csrf.py", line 55, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\django\views\generic\base.py", line 84, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\Admin\Envs\routemaster\lib\site-packages\rest_framework\decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "C:\Users\Admin\Desktop\Locobus\api\adminapi\views.py", line 17, in test
    for r in reader:
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\csv.py", line 110, in __next__
    self.fieldnames
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\csv.py", line 97, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (the file should be opened in text mode)

I am using postman to send the csv file for testing.

Found the Answer at how to upload and read csv file in django using csv.DictReader?

5
  • 1
    Try with with open(csv_file, 'r') as file. Commented Sep 29, 2023 at 13:36
  • Can you also post the complete traceback. Commented Sep 29, 2023 at 13:37
  • added the traceback Commented Sep 29, 2023 at 13:42
  • Can you try reader = csv.DictReader(file.file)? Commented Sep 29, 2023 at 13:49
  • Yes the issue seems to be fixed I just read few minutes ago that csv.DictReader won't work with in memory files so that explains why we needed to decode the file / can also store it locally and then access it Commented Sep 29, 2023 at 14:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.