• Register
0 votes
546 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?)
6.9k points

1 Answer

0 votes

Solution :

I just managed to fixed this issue with my code. The reason behind the exception is the argument rb. Please update that to r.

The Problematic Code:

import csv
icsvfile  = open('sample.csv', "rb")
readcsv = csv.reader(icsvfile)
for row in readcsv :
    print (row) 

New code:

import csv
icsvfile  = open('sample.csv', "r")
readcsv = csv.reader(icsvfile)
for row in readcsv :
    print (row)
36.1k points

Related questions

0 votes
1 answer 746 views
746 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 : ... 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?
asked Jan 2 alecxe 7.5k points
0 votes
1 answer 240 views
240 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 790 views
790 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 161 views
161 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” 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 alecxe 7.5k points
0 votes
1 answer 1.2K views
1.2K 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