python 读取csv文件问题
with open("fer2013.csv", "rb", encoding="utf-8") as vsvfile:
reader = csv.reader(vsvfile)
rows = [row for row in reader]
print(rows)
输出:
Error: iterator should return strings, not bytes (did you open the file in text mode?)
问题分析
因为此csv文件并非二进制文件, 只是一个文本文件。
问题解决
with open("fer2013.csv", "rt", encoding="utf-8") as vsvfile:
reader = csv.reader(vsvfile)
rows = [row for row in reader]
print(rows)
或者
# 因为open()默认打开文本文件
with open("fer2013.csv", "r", encoding="utf-8") as vsvfile:
reader = csv.reader(vsvfile)
rows = [row for row in reader]
print(rows)