I would like to list different stock data which were published in one week. The stock data should all be in seperate arrays. The different array elements should represent the different days and the arrays itself should be the different weeks. So for example week_1[55.5,23.1,234.8,,23.6]
. The code I have tried until now can be viewed below, but unfortunately it simply doesn't work. I always get the error: 'str' object does not support item assignment
. Any simple ideas on how I could fix this?
Python Code:
import datetime
# open csv file
lines = open("google.csv")
lines = lines.read()
lines = lines.split("\n")
i=0
while i<(len(lines)-1):
data = lines[i].split(',')
date2 = data[0].split('-')
week = date(int(date2[0]), int(date2[1]), int(date2[2])).isocalendar()[1]
# Create week array
weekN = "week_"+str(week)
weekN = []
# Get Stock Data
stockN = data[1]
#get day and add stock prices to week array
d = datetime.date(int(date2[0]), int(date2[1]), int(date2[2]))
if d.strftime("%a") == "Mon":
weekN[0] = stockN
if d.strftime("%a") == "Tue":
weekN[1] = stockN
if d.strftime("%a") == "Wed":
weekN[2] = stockN
if d.strftime("%a") == "Thu":
weekN[3] = stockN
if d.strftime("%a") == "Fri":
weekN[4] = stockN
i=i+1
CSV File:
2011-02-07,610.16,618.39,609.21,614.29,1799600,614.29
2011-02-04,610.15,611.44,606.61,610.98,1550800,610.98
2011-02-03,609.48,611.45,606.13,610.15,1495100,610.15
2011-02-02,611.00,614.34,607.53,612.00,1760700,612.00
2011-02-01,604.49,613.35,603.11,611.04,2745300,611.04
2011-01-31,603.60,604.47,595.55,600.36,2804900,600.36
2011-01-28,619.07,620.36,599.76,600.99,4231100,600.99