• Register
0 votes
2.9k views

 Problem :

MySample.csv contains the below details :

NAME    Id    No  Dept
Tommy    1    12   CS
Jimmy    2    35   EC
Bonny    3    21   IT
Franky   4    61   EE

And my Python file contains the below code :

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

But when I try to run the above code in Python, I get the below exception:

File "csvformat.py", line 4, in for row in read : _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

How should I fix it?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You need to open your file in text mode.

More specifically refer below line of code :

myifile  = open('mysample.csv', "rt", encoding=<theencodingofthefile>)

Good guesses for the encoding the "ascii" and "utf8". You can also try to leave the encoding off, and it will just use your system default encoding, which tends to be the UTF8, but may be something else.

OR

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

import csv
import codecs
myifile  = open('mysample.csv', "rb")
read = csv.reader(codecs.iterdecode(myifile, 'utf-8'))
for row in read :
print (row)
9 7 4
38,600 points

Related questions

0 votes
1 answer 11 views
11 views
Problem: how i can move on in dealing with this situation ? Iterator should return strings, not bytes (did you open the file in text mode?.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 1.4K views
1.4K views
Problem : When I run my Python code I get the following exception: _csv.error: iterator should return strings, not bytes (did you open the file in text mode?)
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
1 answer 3 views
3 views
Problem: Who can help me to fix an error showing in my computer that error: iterator should return strings, not bytes (did you open the file in text mode?)
asked Mar 26 ashik 10.7k points
0 votes
1 answer 721 views
721 views
Probem : I am getting bellow error new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 9 views
9 views
Problem: I am facing following error: post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 1.7K views
1.7K views
Problem : I am trying to convert my working Python 2.7 code into Python 3 code but I am facing following error post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 8 views
8 views
Problem: after upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Mar 16 ummesalma 22.9k points
0 votes
1 answer 307 views
307 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: &ldquo;TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'&rdquo; I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2, 2020 alecxe 7.5k points
0 votes
1 answer 2.5K views
2.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
2 answers 125 views
125 views
Problem : I have recently installed the cuda 8.0 and cuda 9.0 in the Gpu support system. I encountered the below error while importing from the keras module. It says it is unable to load the native tensorflow runtime. I have received the error log as below: Traceback ... is, Cuda compilation tools, release 8.0, V8.0.61 Is it the clash between two cuda versions? Have anyone solved this before?
asked Feb 18, 2020 mphil 2.3k points