import csv filename = 'ch02-data.csv'
data = [] try:
with open(filename) as f://用with语句将数据文件绑定到对象f
reader = csv.reader(f)
header = next(reader)//Python 3.X 用的是next()
data = [row for row in reader]
except csv.Error as e:
print('Error reading CSV file at line %s: %s' % (reader.line_num,e) )
sys.exit(-1) if header:
print(header)
print("=======================")
for row in data:
print(row)