+2 votes
in Programming Languages by (8.1k points)

with open(inpfile, 'rb') as csvfile:

dataReader = csv.reader(csvfile, delimiter=',')

for row in dataReader:

print(row)

When I run the above code to read a CSV file, I get the following error: "_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)" I got the syntax from python documentation website. What is wrong in the above code?

1 Answer

0 votes
by (15.8k points)

If you are using Python 3, you should use 'r' instead of 'rb'. 'rb' works only for Python 2.

with open(inpfile, 'r') as csvfile:

dataReader = csv.reader(csvfile, delimiter=',')

for row in dataReader:

print(row)

...