• Register
0 votes
2k 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 1.1K views
1.1K 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 493 views
493 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 1.3K views
1.3K 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 238 views
238 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 1.9K views
1.9K 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
1 answer 5.7K views
5.7K views
Problem : I am getting following error: pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it's not in your path
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
2 answers 85 views
85 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
1 vote
1 answer 56 views
56 views
Problem : I want to learn the python programming and also want to work on projects. One of my projects is to code the voting system so that it can select the best player among 23 players of a match by using the lists. I am currently using the Python3. Please ... any of the strings in my above code all my variables are of the type integers. Please guide me in fixing above python related error.
asked May 14, 2020 Martin K 6.6k points
1 vote
2 answers 99 views
99 views
Problem : The program supposed to take in two names, and if they are the same length it should check if they are the same word. But i am getting below error in python python typeerror: not all arguments converted during string formatting
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
2 answers 420 views
420 views
Problem : I want to read my .csv file into Python (Spyder) but I am facing the error. Please find below my code : import csv mydata = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener") mydata = csv.reader(mydata) print(mydata) I face the following error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
asked Dec 6, 2019 alecxe 7.5k points