In the following code, row[8], row[9], ..., row[27] each contain many numbers. I need all of the numbers in row[8], row[9], etc. to append to stat_by_symbol as separate lists within within stat_by_symbol. However, the code below appends all of the numbers across all rows within a single list.
As an example of what I need, if I called stat_by_symbol['aaa'][0], then I should get the list of numbers pulled from row[8].
How can I fix this? Many thanks.
EDIT for further clarification. I've attached a snapshot of the csv. I need stat_by_symbol['aaa'][0] to give me all of the numbers under Column i. Similarly, stat_by_symbol with index 1 would give me all of the numbers under Column j.
stat_by_symbol = {}
with open('zzdata.csv', 'rb') as f:
reader = csv.reader(f)
reader.next()
for row in reader:
for symbol in symbols:#symbols in a list
if symbol in row:
for i in range(8, 28):
stat_by_symbol.setdefault(symbol, []).append(row[i])
解决方案stat_by_symbol = dict((symbol, [[] for i in xrange(8,28)]) for symbol in symbols)
with open('zzdata.csv', 'rb') as f:
reader = csv.reader(f)
reader.next()
for row in reader:
for symbol, symbol_list in stat_by_symbol.iteritems():
if symbol in row:
for symbol_list2, cell in zip(symbol_list, row[8:28]):
symbol_list2.append(cell)