导入CSV文件导入数据的步骤
①打开xxx.csv文件
②首先读取文件头
③然后读取剩余头
④当发生错误时抛出异常
读取完所有内容后,打印文件头和剩余所有行
代码:
import csv
filename = 'enrollments.csv'
data = []
try:
with open(filename)as f:
reader = csv.reader(f)
header = next(reader)
data = [row for row in reader]
except:
pass
if header:
print(header)
print('=========================')#分隔行
for datarow in data:
print(datarow)