读取.csv文件,文件内容如下:
NAME Id No Dept
Tom 1 12 CS
Hendry 2 35 EC
Bahamas 3 21 IT
Frank 4 61 EE
python代码:
import csv
ifile = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
print (row)
运行后:却得到:
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?)
解决方案:
源代码中 ‘rb'
改为’r'
原始代码:
import csv
ifile = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
print (row)
修改后:
import csv
ifile = open('sample.csv', "r")
read = csv.reader(ifile)
for row in read :
print (row)