+2 votes
in Programming Languages by (73.8k 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

+1 vote
by (349k points)
selected by
 
Best answer

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)


...