NAME i would No DeptTom 1 12 CSHendry 2 35 ECBahamas 3 21 ITFrank 4 61 EEAnd the Python record contains the adhering to code:

import csvifile = open("sample.csv", "rb")read = csv.reader(ifile)for heat in check out : print (row) once I run the above code in Python, I acquire the following exception:

File "", heat 4, in for heat in check out : _csv.Error: iterator must return strings, no bytes (did you open up the document in text mode?)

How deserve to I settle it?


You open the paper in message mode.

You are watching: _csv.error: iterator should return strings, not bytes (did you open the file in text mode?)

More specifically:

ifile = open("sample.csv", "rt", encoding=)Good guesses for encoding is "ascii" and "utf8". You can additionally leave the encoding off, and it will usage the system default encoding, which often tends to be UTF8, however may be something else.


The reason it is throw that exception is since you have actually the debate rb, which opens the paper in binary mode. Readjust that come r, which will certainly by default open up the record in message mode.

Your code:

import csvifile = open("sample.csv", "rb")read = csv.reader(ifile)for row in review : print (row) brand-new code:

import csvifile = open("sample.csv", "r")read = csv.reader(ifile)for heat in read : publish (row)



In Python3, csv.reader expects, that passed iterable returns strings, no bytes. Below is one much more solution to this problem, that provides codecs module:

import csvimport codecsifile = open("sample.csv", "rb")read = csv.reader(codecs.iterdecode(ifile, "utf-8"))for heat in review : publish (row)


Your difficulty is you have the b in the open flag. The flag rt (read, text) is the default, so, using the context manager, simply do this:

with open("sample.csv") as ifile: check out = csv.reader(ifile) for row in read: publish (row) The context manager method you don"t require generic error handling (without which you may get stuck through the record open, particularly in an interpreter), since it will instantly close the file on an error, or top top exiting the context.

The over is the exact same as:

with open("sample.csv", "r") together ifile: ...or

with open("sample.csv", "rt") together ifile: ...
I had this error when running an old python script developped through Python 2.6.4

When updating come 3.6.2, I had actually to remove all "rb" parameters from open calls in stimulate to deal with this csv analysis error.

Thanks because that contributing solution to stack Overflow!

Please be sure to answer the question. Carry out details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based upon opinion; ago them up with references or personal experience.

See more: Pokemon X First Gym 1 - Pokemon X & Pokemon Y Walkthrough

To discover more, view our advice on writing an excellent answers.

write-up Your price Discard

By clicking “Post her Answer”, you agree come our terms of service, privacy policy and cookie plan

site architecture / logo © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.12.22.41046

her privacy

By click “Accept every cookies”, girlfriend agree stack Exchange deserve to store cookie on your an equipment and disclose information in accordance v our Cookie Policy.