Python Data Visualization Cookbook 2.2.2

 1 import csv
 2 
 3 filename = 'ch02-data.csv'
 4 data = []
 5 
 6 try:
 7     with open(filename) as f://用with语句将数据文件绑定到对象f
 8         reader = csv.reader(f)
 9         header = next(reader)//Python 3.X 用的是next()
10         data = [row for row in reader]
11 except csv.Error as e:
12     print('Error reading CSV file at line %s: %s' % (reader.line_num,e) )
13     sys.exit(-1)
14 
15 if header:
16     print(header)
17     print("=======================")
18 for row in data:
19     print(row)

 

posted @ 2016-11-11 23:13  barriery  阅读(291)  评论(0编辑  收藏  举报