python机器可读数据-csv
逗号分隔值(Comma-Separated Values,CSV)
1 csv数据
还有一种数据类型,叫制表分隔值(tab-separated values,TSV)数据,有时与CSV归为一类。
若文件扩展名是.tsv,则文件很可能是TSV数据
若文件扩展名是.csv,则既可能是TSV,又可能是CSV
导入CSV数据
import csv csvFile = open('600050.csv','r') #'r'只读 read = csv.reader(csvFile) for row in read: print(row)
得到一组列表(csv.reader)形式的数据,这是一组股票数据。
['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'] ['2017-10-09', '7.510000', '7.530000', '7.260000', '7.290000', '7.290000', '420944045'] ['2017-10-10', '7.270000', '7.320000', '7.150000', '7.200000', '7.200000', '335699391'] ['2017-10-11', '7.180000', '7.200000', '7.080000', '7.150000', '7.150000', '271275437'] ['2017-10-12', '7.190000', '7.410000', '7.140000', '7.320000', '7.320000', '403631829'] ['2017-10-13', '7.270000', '7.300000', '7.220000', '7.250000', '7.250000', '171056516'] ['2017-10-16', '7.280000', '7.380000', '7.200000', '7.210000', '7.210000', '201027736'] ['2017-10-17', '7.180000', '7.240000', '7.150000', '7.210000', '7.210000', '117206970'] ['2017-10-18', '7.210000', '7.260000', '7.130000', '7.160000', '7.160000', '134849702'] ['2017-10-19', '7.150000', '7.150000', '6.920000', '6.920000', '6.920000', '279917433'] ['2017-10-20', '6.970000', '7.030000', '6.950000', '6.990000', '6.990000', '153638424'] ['2017-10-23', '6.980000', '7.150000', '6.980000', '7.120000', '7.120000', '218381016'] ['2017-10-24', '7.100000', '7.110000', '7.010000', '7.060000', '7.060000', '129866126'] ['2017-10-25', '7.060000', '7.150000', '7.020000', '7.080000', '7.080000', '119502092'] ['2017-10-26', '7.060000', '7.150000', '7.050000', '7.130000', '7.130000', '164961266'] ['2017-10-27', '7.200000', '7.520000', '7.200000', '7.350000', '7.350000', '621619370'] ['2017-10-30', '7.350000', '7.650000', '7.250000', '7.650000', '7.650000', '708679107'] ['2017-10-31', '7.600000', '7.680000', '7.510000', '7.640000', '7.640000', '372664620']
还可以得到字典形式的数据(csv.DictReader)
import csv csvFile = open('600050.csv','r') #'r'只读 read = csv.DictReader(csvFile) for row in read: print(row)
得到:
{'High': '7.530000', 'Adj Close': '7.290000', 'Close': '7.290000', 'Low': '7.260000', 'Open': '7.510000', 'Date': '2017-10-09', 'Volume': '420944045'} {'High': '7.320000', 'Adj Close': '7.200000', 'Close': '7.200000', 'Low': '7.150000', 'Open': '7.270000', 'Date': '2017-10-10', 'Volume': '335699391'} {'High': '7.200000', 'Adj Close': '7.150000', 'Close': '7.150000', 'Low': '7.080000', 'Open': '7.180000', 'Date': '2017-10-11', 'Volume': '271275437'} {'High': '7.410000', 'Adj Close': '7.320000', 'Close': '7.320000', 'Low': '7.140000', 'Open': '7.190000', 'Date': '2017-10-12', 'Volume': '403631829'} {'High': '7.300000', 'Adj Close': '7.250000', 'Close': '7.250000', 'Low': '7.220000', 'Open': '7.270000', 'Date': '2017-10-13', 'Volume': '171056516'} {'High': '7.380000', 'Adj Close': '7.210000', 'Close': '7.210000', 'Low': '7.200000', 'Open': '7.280000', 'Date': '2017-10-16', 'Volume': '201027736'} {'High': '7.240000', 'Adj Close': '7.210000', 'Close': '7.210000', 'Low': '7.150000', 'Open': '7.180000', 'Date': '2017-10-17', 'Volume': '117206970'} {'High': '7.260000', 'Adj Close': '7.160000', 'Close': '7.160000', 'Low': '7.130000', 'Open': '7.210000', 'Date': '2017-10-18', 'Volume': '134849702'} {'High': '7.150000', 'Adj Close': '6.920000', 'Close': '6.920000', 'Low': '6.920000', 'Open': '7.150000', 'Date': '2017-10-19', 'Volume': '279917433'} {'High': '7.030000', 'Adj Close': '6.990000', 'Close': '6.990000', 'Low': '6.950000', 'Open': '6.970000', 'Date': '2017-10-20', 'Volume': '153638424'} {'High': '7.150000', 'Adj Close': '7.120000', 'Close': '7.120000', 'Low': '6.980000', 'Open': '6.980000', 'Date': '2017-10-23', 'Volume': '218381016'} {'High': '7.110000', 'Adj Close': '7.060000', 'Close': '7.060000', 'Low': '7.010000', 'Open': '7.100000', 'Date': '2017-10-24', 'Volume': '129866126'} {'High': '7.150000', 'Adj Close': '7.080000', 'Close': '7.080000', 'Low': '7.020000', 'Open': '7.060000', 'Date': '2017-10-25', 'Volume': '119502092'} {'High': '7.150000', 'Adj Close': '7.130000', 'Close': '7.130000', 'Low': '7.050000', 'Open': '7.060000', 'Date': '2017-10-26', 'Volume': '164961266'} {'High': '7.520000', 'Adj Close': '7.350000', 'Close': '7.350000', 'Low': '7.200000', 'Open': '7.200000', 'Date': '2017-10-27', 'Volume': '621619370'} {'High': '7.650000', 'Adj Close': '7.650000', 'Close': '7.650000', 'Low': '7.250000', 'Open': '7.350000', 'Date': '2017-10-30', 'Volume': '708679107'} {'High': '7.680000', 'Adj Close': '7.640000', 'Close': '7.640000', 'Low': '7.510000', 'Open': '7.600000', 'Date': '2017-10-31', 'Volume': '372664620'}