CSV是Comma-Separated Values的缩写,用文本文件形式储存的表格数据。
就可以存储为csv文件,文件内容是:
1 2 3 4 5
| no.,name,age,score 1,apple,12,98 2,ben,13,97 3,celia,14,96 4,dave,15,95
|
csv文件保存为”a.csv”,如何用Python像操作Excel一样提取其中的一列,可以利用Python自带的csv模块,有两种方法可以实现:
第一种方法使用reader函数,接收一个可迭代的对象(比如csv文件),能返回一个生成器,就可以从其中解析出csv的内容:比如下面的代码可以读取csv的全部内容,以行为
1 2 3 4
| import csv with open('a.csv','rb') as f: reader = csv.reader(f) rows = [row for row in reader]
|
得到如下结果
1 2 3 4 5
| [['no.', 'name', 'age', 'score'], ['1', 'apple', '12', '98'], ['2', 'ben', '13', '97'], ['3', 'celia', '14', '96'], ['4', 'dave', '15', '95']]
|
要获取其中一列
1 2 3 4 5 6
| import csv with open('a.csv','rb') as csvfile: reader = csv.reader(csvfile) column = [row[2] for row in reader] print column
|
得到如下结果
1
| ['age', '12', '13', '14', '15']
|
PS:从csv读出的都是str类型。这种方法要事先知道列的序号,比如age在第2列,而不能根据’age’这个标题查询。
第二种方法是使用dictreader,和reader函数类似,接收一个可迭代的对象,能返回一个生成器,但是返回的每一个单元格都放在一个字典的值内,而这个字典的键则是这个单元格的标题(即列头)。用下面的代码可以看到dictreader的结构:
1 2 3 4 5
| import csv with open('a.csv','rb') as csvfile: reader = csv.dictreader(csvfile) column = [row for row in reader] print column
|
得到如下结果
1 2 3 4
| [{'age': '12', 'no.': '1', 'score': '98', 'name': 'apple'}, {'age': '13', 'no.': '2', 'score': '97', 'name': 'ben'}, {'age': '14', 'no.': '3', 'score': '96', 'name': 'celia'}, {'age': '15', 'no.': '4', 'score': '95', 'name': 'dave'}]
|
如果我们想用dictreader读取csv的某一列,就可以用列的标题查询:
1 2 3 4 5
| import csv with open('a.csv','rb') as csvfile: reader = csv.dictreader(csvfile) column = [row['age'] for row in reader] print column
|
得到如下结果
1
| ['12', '13', '14', '15']
|