csv文件如下所示:
按行读取csv文件
(1)reader函数
import csv
with open('test.csv','rt') as csvfile: #此csv文件是一个文本文件,并非二进制文件,如是二进制文件,'rt'改为'rb'
reader = csv.reader(csvfile)
rows = [row for row in reader]
print (rows)
结果:
如果想读取特定的行:
import csv
with open('test.csv','rt') as csvfile:
reader = csv.reader(csvfile)
for i,rows in enumerate(reader):
if i == 2:
row = rows
print (row)
结果:
['3', '4', '5', '3