今天在读一个csv文件的时候遇到一个以前没碰到过的报错:
with open('zhihu.csv','rb') as csvfile:
reader = csv.reader(csvfile)
column = [row[2] for row in reader]
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
说这个csv文件是一个txt文件,不是二进制文件
解决方法:只要把’rb’改成’rt’即可解决。
with open('zhihu.csv','rt') as csvfile:
reader = csv.reader(csvfile)
column = [row[2] for row in reader]