-1

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
Philipp Braun
  • 1,583
  • 2
  • 25
  • 41
  • It's because `weekN` is a string. When you're doing `weekN[1] = stockN`, for example, what you're effectively telling Python is to try changing the character at index 1 to be whatever `stockN` is. However, in Python, strings are immutable -- they can't be changed. If you meant for `weekN` to be a list, then you have a bug you need to fix. If you do want to modify a string, you'll have to adjust your program so that you always return a new string instead of attempting to modify one. – Michael0x2a Jul 27 '14 at 19:23
  • but if i would create a new array the program would not work as well – Philipp Braun Jul 27 '14 at 19:24

1 Answers1

0

In the following code:

  • a dictionary is created, having keys like 'week_1'
  • each value is also a dictionary
  • use with, iterate over the lines of the file, parse dates using the standard library contents

Thus we get

import datetime
import time
from collections import defaultdict
weeks = defaultdict(dict)

with open('google.csv') as f:
    for line in f:
        data = line.split(',')
        #get week
        the_date = datetime.date(*time.strptime(data[0], '%Y-%m-%d')[:3])

        _, week_number, day_number = the_date.isocalendar()
        week = "week_{}".format(week_number)
        stock = data[1]

        if day_number in range(1, 6):
            weeks[week][day_number - 1] = stock

Result:

{'week_4': {4: '619.07'},
 'week_5': {0: '603.60', 1: '604.49', 2: '611.00', 3: '609.48', 4: '610.15'},
 'week_6': {0: '610.16'}}