IndexError: list index out of range but I have more than enough elements?! Python -
def build_country_dict(lines): '''return dictionary in form of {country: count}, country country code , count number of medals won athletes country''' d = {} #start empty dictionary in range(1, len(lines)): #for ranging 1 length of lines line_list = lines[i].split(',') # split lines[i] list - split on comma country = line_list[6] # country if country not in d: # if country not in dictionary d[country] = 0 # add count of 0 d[country] = d[country] + 1 # add 1 country count return d #return dictionary
the error indexerror: list index out of range
referring line_list[6]
list using has 11 elements in have no idea how correct this.
the original file cvs file, using excel checked through whole thing , every single line should turn list containing 11 elements. file large me print lists.
i did try smaller portion print(line_list[6]) , printed fine.
lines[i]
doesn't seem have sufficient data split ','.
try code instead of country = line_list[6]
country = line_list[6] if len(line_list) > 6 else 'unknown'
alternatively,
try: country = line_list[6] except indexerror: continue
this fits situation more.
Comments
Post a Comment