I keep getting "List index out of range" when trying to execute this code, but for the life of me I cannot figure out why, If anyone has any experience coding linux gDesklets this would help.

File: StockWatcher.display

...                                                                   
parser.dataSet = stockInfo.html  
...

stockInfo.html is an object containing a CSV string "Some Company,127.2,+0.33%"


File: __init__.py

...
def __setData(self, csvData):
	for row in csv.reader(csvData):
		self.__name = row[0]
		self.__currentvalue = row[1]
		self.__changepercent = row[2]
		if (match("+", row[2])):
			self.__changecolor = "green"
			self.__imagename = "gfx/gain.png"
		if (match("-", row[2])):
			self.__changecolor = "red"
			self.__imagename = "gfx/loss.png"
...
dataSet = property(None, __setData, None)
...

Any thoughts?

Thanks!

Hi,

If anyone has any experience coding linux gDesklets this would help

I seriously dont know about this stuff for sure.

I keep getting "List index out of range" when trying to execute this code, but for the life of me I cannot figure out why,
...
Any thoughts?

sure...
Usually IndexError: list index out of range exception occurs when you try to refer a list with index with which does not exist.

>>> li = [1,2,3,4] # list having 4 elements
>>> li[4]  # trying to access 5th element, which does not exist

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    li[4]
IndexError: list index out of range
>>>

As i see from your code, you are passing open file handle to the function. And read through it, which returns a list and assigning it to some private class members. From the above you can see, Python clearly points which is line-number and what statement is causing that exception, if you see clearly li[4](from the above eg.). You might check list index by your self, or may be in some rows of CSV file, there may not be +0.33%(from you eg.) value. so better to pack your code which actually refers list in a try-except block.

>>> try:
	li[4]
except IndexError, e:
	print e# or pass, do nothing just ignore that row...

Hope this helps...

kath.

Hi,

I seriously dont know about this stuff for sure.
sure...
Usually IndexError: list index out of range exception occurs when you try to refer a list with index with which does not exist.

>>> li = [1,2,3,4] # list having 4 elements
>>> li[4]  # trying to access 5th element, which does not exist

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    li[4]
IndexError: list index out of range
>>>

As i see from your code, you are passing open file handle to the function. And read through it, which returns a list and assigning it to some private class members. From the above you can see, Python clearly points which is line-number and what statement is causing that exception, if you see clearly li[4](from the above eg.). You might check list index by your self, or may be in some rows of CSV file, there may not be +0.33%(from you eg.) value. so better to pack your code which actually refers list in a try-except block.

>>> try:
	li[4]
except IndexError, e:
	print e# or pass, do nothing just ignore that row...

Hope this helps...

kath.

Thats the weird thing, even if I change the __setData function to simply set a test variable to "test" I get this error. So this error seems to pop up for the line "parser.dataSet = stockInfo.html" even when the funtcion stockInfo.html is being passed to doesn't contain any indexes.

Thats the weird thing, even if I change the __setData function to simply set a test variable to "test" I get this error.

yeah!

So this error seems to pop up for the line "parser.dataSet = stockInfo.html" even when the funtcion stockInfo.html is being passed to doesn't contain any indexes.

Better post your code.. how in the world can anyone come to know you have you problem in your code... try to be very clear.
post your code...

kath.

The same code is being print, when the sys.argv[] function hasn't been done.

#!/usr/bin/env python
#-*- coding: utf-8 -*-

#test.py
#run it like this:
#python test.py <some_string> 
#where <some_string> is random letter, which you will write

import sys

test_argv = sys.argv[1]
if test_argv != '':
	print "Hello World!"

Try not to write anything, and you''l get same Traceback with index out of range. It can be not related with your error, it's only example. I think you should search for this problem on GDesklet's site, or report error to the developers.

I have a problem where i get this error message but for the life of me I cannot figure out why.
My lists are:

[1, 2, 3]                                                                       
['Ace of Spades', '8 of Hearts', '2 of Hearts', '5 of Diamonds', 'Ace of Clubs']

and a "deck" of length 47.
My intention is to change the cards in the 2nd list(the hand) of the inputed positions in the first list using this code, where change is the first list, self.hand is the second, and self.deck is the "deck":

for i in change:
            self.deck.append(self.hand[i])
            del self.hand[i]
for j in range(5-len(change)):
            self.hand.append(self.deck[j])
            del self.deck[j]

When I try to run it, I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "poker.py", line 244, in __init__
self.playRound()
File "poker.py", line 258, in playRound
self.deck.append(self.hand)
IndexError: list index out of range

It doesn't make sense. I checked, self.hand is definitely of length 5.

Even i am getting this problem . my python code is

 for i,j in nltk.pos_tag(words):
        print i,j
        if 'JJ' in j:
            pj=(list(swn.senti_synsets(i,'a'))[0]).pos_score()
            print "pj:" ,pj
        elif 'RB' in j:
            pr =(list(swn.senti_synsets(i,'r'))[0]).pos_score()
            print "pr:" ,pr
        elif 'NN' in j:
            pn =(list(swn.senti_synsets(i,'n'))[0]).pos_score()
            print "pn:" ,pn
        elif 'VB' in j:
            pv =(list(swn.senti_synsets(i,'v'))[0]).pos_score()
            print "pv:" ,pv

where "words" is a list of words containing adjective , adverb , noun etc..
words=['good','only','excellent','really'.........] nearly 300 such words. when i run this code it works for first 2 words .that is positive score of good and only will b displayed. but when it goes to word "excellent " there is this error.. please help !!!!

Edited 9 Months Ago by lavanyarao

@lavanyarao list index out of range is a very general message. You must post the whole error traceback to see which list is involved. Also, it is better if you start your own forum thread with the tag python instead of reviving an old thread.

Comments
my error message : Traceback (most recent call last): File "C:\Python27\mat to file.py", line 39, in pj+=(swn.senti_synsets(i,'a')[0]).
This question has already been answered. Start a new discussion instead.