pshmell
pshmell

Reputation: 11

Python 3.3 IndexError doesn't make sense

I have a multidimensional array in py3.3, looks like

[ # big container

  [ # month container

    ['date string', 'temp'],['date string', 'temp'] #dates in month

  ], #close month container

  [ # next month container

    ['date string', 'temp'], ['date string', 'temp']

  ]

]

Here is my code:

dailyDict = []
dailyRow = []
compareStation = 'myfile.csv'
with open(compareStation, newline='\n') as csvfile:
            station = csv.reader(csvfile, delimiter=',', quotechar='|')
            for row in station:
                if 1stDayOfMonthCondition:
                    dailyDict.append(dailyRow)
                    dailyRow = []
                    dailyRow.append(row)
                else:
                    dailyRow.append(row)

for month in dailyDict:
        print(month[1])

this gives me an IndexError, list index out of range. However, when I run print(month) I get each month printed out just fine.

And when I set the printed month as a variable, say, x, in the shell, I can print(x[1]) just fine. But print(month[1]) still fails. Very confused.

Thanks.

Upvotes: 1

Views: 45

Answers (1)

TheNiv
TheNiv

Reputation: 13

The index list starts at 0 not 1 so you should try print(month[0])to see if you get any thing that way

Upvotes: 1

Related Questions