# admin csv import csv.Error: iterator should return strings, not bytes

**URL:** <https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114>\
**Category:** Using Django\
**Created:** [November 6, 2020, 5:40pm UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114 "2020-11-06T17:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ad-software](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ad-software/32/2350_2.png) [@ad-software](https://forum.djangoproject.com/u/ad-software)\
**Post date:** [November 6, 2020, 5:40pm UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114/1 "2020-11-06T17:40:27Z")

</div>

Hi,  
I created an import for csv in admin area, but I get the error  
File “D:\andreas\Programmierung\Python\bestenliste\_laufen\running\_results\distances\admin.py”, line 30, in import\_csv  
for row in reader:  
\_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

I followed the guide

> **[5. How to import CSV using Django admin? — Django Admin Cookbook 2.0...](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/import.html)**

admin.py  
csv\_file = request.FILES[“csv\_file”]  
reader = csv.reader(csv\_file)  
# todo get highest sort number  
sort = 1  
for row in reader:

Here is the complete code

> <https://github.com/ad-software/running_results/blob/master/running_results/distances/admin.py>

Can you please help me?

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [November 7, 2020, 5:59am UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114/2 "2020-11-07T05:59:53Z")

</div>

The problem is that csv.reader expects something that returns strings, and you’re getting bytes from request.FILES. I found TextIOWrapper in the standard library (docs here: [https://docs.python.org/3/library/io.html#io.TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper)) which seems might help here:

```auto
import io

with io.TextIOWrapper(request.FILES["csv_file"].chunks(), encoding="utf-8") as text_file:
    reader = csv.reader(text_file)

```

You may need to add the `delimiter` keyword argument to csv.reader depending on what the separator is.

Those with more experience with request.FILES may say I’m wrong, and I’m perfectly fine with that.

Hope this helps!

-Jorge

---

<div class="post-metadata">

**Author:** ![ad-software](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ad-software/32/2350_2.png) [@ad-software](https://forum.djangoproject.com/u/ad-software)\
**Post date:** [November 7, 2020, 7:07am UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114/3 "2020-11-07T07:07:58Z")

</div>

Hi, I tried

```
with io.TextIOWrapper(request.FILES["csv_file"].chunks(), encoding="utf-8") as text_file:
reader = csv.reader(text_file, delimiter=';')

```

but I get the error:  
File “D:\andreas\Programmierung\Python\bestenliste\_laufen\running\_results\distances\admin.py”, line 33, in import\_csv  
with io.TextIOWrapper(request.FILES[“csv\_file”].chunks(), encoding=“utf-8”) as text\_file:  
AttributeError: ‘generator’ object has no attribute ‘readable’

---

<div class="post-metadata">

**Author:** ![claudep](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/claudep/32/5377_2.png) [@claudep](https://forum.djangoproject.com/u/claudep)\
**Post date:** [November 7, 2020, 1:48pm UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114/4 "2020-11-07T13:48:24Z")

</div>

I had to do that recently, here’s my approach:

```
import csv    
from io import StringIO

content = StringIO(uploaded_file.read().decode('latin-1'))
csv_reader = csv.reader(content, delimiter=';')
```

---

<div class="post-metadata">

**Author:** ![ad-software](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ad-software/32/2350_2.png) [@ad-software](https://forum.djangoproject.com/u/ad-software)\
**Post date:** [November 7, 2020, 6:02pm UTC](https://forum.djangoproject.com/t/admin-csv-import-csv-error-iterator-should-return-strings-not-bytes/5114/6 "2020-11-07T18:02:23Z")

</div>

Thank you for the help! Now it is working:

```
with io.TextIOWrapper(request.FILES["csv_file"], encoding="utf-8", newline='\n') as text_file:
       reader = csv.reader(text_file, delimiter=';')                
       for row in reader:
```
