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

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

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

Can you please help me?

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) which seems might help here:

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

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’

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=';')
1 Like

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:
2 Likes