close

[Solved] csv.Error: iterator should return strings, not bytes

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error csv.Error: iterator should return strings, not bytes in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How csv.Error: iterator should return strings, not bytes Error Occurs?

Today I get the following error csv.Error: iterator should return strings, not bytes in python.

How To Solve csv.Error: iterator should return strings, not bytes Error ?

  1. How To Solve csv.Error: iterator should return strings, not bytes Error ?

    To Solve csv.Error: iterator should return strings, not bytes Error

  2. csv.Error: iterator should return strings, not bytes

Solution 1

The reason it is throwing that exception is because you have the argument rb, which opens the file in binary mode. Change that to r, which will by default open the file in text mode.

Your code:

import csv
ifile  = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
    print (row) 

New code:

import csv
ifile  = open('sample.csv', "r")
read = csv.reader(ifile)
for row in read :
    print (row)

Solution 2


In Python3, csv.reader expects, that passed iterable returns strings, not bytes. Here is one more solution to this problem, that uses codecs module:

import csv
import codecs
ifile  = open('sample.csv', "rb")
read = csv.reader(codecs.iterdecode(ifile, 'utf-8'))
for row in read :
    print (row) 

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read