简体   繁体   中英

Python for loop IndexError: list index out of range

I am somewhat new to python and I could use some help figuring out what my error entails.

datafile = "filename.csv"

colors = ['Green','White','Yellow','Pink','Blue','Purple','Gray','Brown','Orange','Red','Black']  # put in order of the columns

colnames = []
for color in colors:
    colnames.append(color+"1")
    colnames.append(color+"2")

# Define data structure to hold responses for each column
responses = dict()
for colname in colnames:
    responses[colname] = []

# Read in the responses
lines = open(datafile,"r").readlines()
for line in lines:
    data = line.strip('\n').split(",")
for i,colname in enumerate(colnames):
    if '1' in colname:
        responses[colname].append(data[i])
    else:
        responses[colname].append(data[i].split(','))

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-21-4039ddb1f5f5> in <module>()
     21             responses[colname].append(data[i])
     22         else:
---> 23             responses[colname].append(data[i].split(','))
     24 
     25 # Count the number of subjects

     IndexError: list index out of range

I am not sure if the IndexError: list index out of range is from the actual else: or something is wrong with the responses[colname].append(data[i].split(',')) . It could also be in my for loop of the list colnames , I am fairly certain that aspect is correct, but you never know

for line in lines:
    data = line.strip('\n').split(",")

I think the above code is why you got the IndexError ,because the assignment to data will overwrite the previous one, and the content of data is always the last line . (Unless you just want the last line).

eg:

filename.csv

blue,red
red,green
yellow,red

data=['yellow', 'red']

That means your for loop is equivalent to data=lines[-1].strip('\\n').split(",")

So I suppose this is what you want:

data=[]
for line in lines:
    data.append(line.strip('\n').split(","))

Or use this list comprehension:

data=[line.strip('\n').split(",") for l in lines]

And then make sure that the length of data is greater than colnames . By the way, try to debug your code, or print variables, you will find the reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM