CSV (逗号分隔值(Comma-Separated Values,CSV)文件)(简称为 CSV)是指将数据列用逗号分隔的文件。文件的扩展名是 .csv。另一种数据类型,叫作制表符分隔值(tab-separated values,TSV)数据, 有时也与 CSV归为一类。TSV 与 CSV 唯一的不同之处在于,数据列之间的分隔符是制表符(tab), 而不是逗号。文件的扩展名通常是 .tsv,但有时也用 .csv 作为扩展名。从本质上来看, .tsv 文件与 .csv 文件在 Python 中的作用是相同的。
在文本编辑器中打开 CSV 文件,你看到的数据应该与其类似:
"Year","Country","Sex","Display Value","Numeric"
"1990","Andorra","Both sexes","77","77.00000"
"2000","Andorra","Both sexes","80","80.00000"
"2012","Andorra","Female","28","28.00000"
"2000","Andorra","Both sexes","23","23.00000"
"2012","United Arab Emirates","Female","78","78.00000"
"2000","Antigua and Barbuda","Male","72","72.00000"
"1990","Antigua and Barbuda","Male","17","17.00000"
"2012","Antigua and Barbuda","Both sexes","22","22.00000"
"2012","Australia","Male","81","81.00000"
下面用 Python (运行环境python2.7)打开这个文件,并将数据转换成
Python 可以理解的格式。这只要几行代码:
1 import csv 2 csvfile = open('/home/lwk/python_test_data/data_processing/scores.csv', 'rb') 3 reader = csv.reader(csvfile) 4 for row in reader: 5 print row