我正在处理超过 6MM 行的股票代码数据。我想获取一个符号的所有数据,进行我需要的处理,然后输出结果。
我编写了代码,告诉我每个代码从哪一行开始(参见下面的代码)。我认为如果我知道新符号的开始位置(而不是行号)会更有效,这样我就可以使用 seek(#) 轻松跳转到代码的起始位置。我也很好奇如何扩展此逻辑以读取整个数据块(start_position 到 end_position)以获取股票代码。
import csv
data_line = 0 # holds the file line number for the symbol
ticker_start = 0
ticker_end = 0
cur_sec_ticker = ""
ticker_dl = [] # array for holding the line number in the source file for the start of each ticker
reader = csv.reader(open('C:\\temp\sample_data.csv', 'rb'), delimiter=',')
for row in reader:
if cur_sec_ticker != row[1]: # only process a new ticker
ticker_fr = str(data_line) + ',' + row[1] # prep line for inserting into array
# desired line for inserting into array, ticker_end would be the last
# of the current ticker data block, which is the start of the next ticker
# block (ticker_start - 1)
#ticker_fr = str(ticker_start) + str(ticker_end) + str(data_line) + ',' + row[1]
print ticker_fr
ticker_dl.append(ticker_fr)
cur_sec_ticker = row[1]
data_line += 1
print ticker_dl
下面我放了一个数据文件的小样本:
seq,Symbol,Date,Open,High,Low,Close,Volume,MA200Close,MA50Close,PrimaryLast,filter_$
1,A,1/1/2008,36.74,36.74,36.74,36.74,0, , ,1,1
2,A,1/2/2008,36.67,36.8,36.12,36.3,1858900, , ,1,1
3,A,1/3/2008,36.3,36.35,35.87,35.94,1980100, , ,1,1
1003,AA,1/1/2008,36.55,36.55,36.55,36.55,0, , ,1,1
1004,AA,1/2/2008,36.46,36.78,36,36.13,7801600, , ,1,1
1005,AA,1/3/2008,36.18,36.67,35.74,36.19,7169000, , ,1,1
2005,AAN,4/20/2009,20,20.7,18.2067,18.68,808700, , ,1,1
2006,AAN,4/21/2009,18.7,19.06,18.6533,18.9933,530200, , ,1,1
2007,AAN,4/22/2009,19.2867,19.6267,18.54,19.1333,801100, , ,1,1
2668,AAP,1/1/2008,37.99,37.99,37.99,37.99,0, , ,1,1
2669,AAP,1/2/2008,37.99,38.15,37.17,37.59,1789200, , ,1,1
2670,AAP,1/3/2008,37.58,38.16,37.35,37.95,1584700, , ,1,1
3670,AAR,1/1/2008,22.94,22.94,22.94,22.94,0, , ,1,1
3671,AAR,1/2/2008,23.1,23.38,22.86,23.15,17100, , ,1,1
3672,AAR,1/3/2008,23,23,22,22.16,45600, , ,1,1
6886,ABB,1/1/2008,28.8,28.8,28.8,28.8,0, , ,1,1
6887,ABB,1/2/2008,29,29.11,28.23,28.64,4697700, , ,1,1
6888,ABB,1/3/2008,27.92,28.35,27.79,28.08,5240100, , ,1,1