python - IndexError: list index out of range: I can uderstand why this error happens -


i got error indexerror: list index out of range. wanna put dictionary data model(user) . wrote

book2 = xlrd.open_workbook('./data/excel1.xlsx') sheet2 = book2.sheet_by_index(0) row_index in range(1,sheet2.nrows):     rows = sheet2.row_values(row_index)     print(rows)     user3 = user.objects.filter(user_id=rows[0])     if user3:         user3.update(talk1=rows[2],characteristic1=rows[3],talk2=rows[4],characteristic2=rows[5],                      talk3=rows[6], characteristic3=rows[7],talk4=rows[8], characteristic4=rows[9],                      talk5=rows[10], characteristic5=rows[11],talk6=rows[12], characteristic6=rows[13],                      talk7=rows[14], characteristic7=rows[15], talk8=rows[16], characteristic8=rows[17]) 

however,the number of rows's index different each list.rows like

['001200cd3ef', 'tom', 'hi', 'greeting', 'goodmorning', 'greeting', 'bye' 'say good‐bye', '', '', '']['007700ab7ws', 'blear', 'how you', 'greeting', 'thx', 'thanks', '', '', ''] 

so each list's number of index different,max index 13. models.py is

class user(models.model):     talk1 = models.charfield(max_length=500,null=true)     talk2 = models.charfield(max_length=500, null=true)     talk3 = models.charfield(max_length=500, null=true)     talk4 = models.charfield(max_length=500, null=true)     talk5 = models.charfield(max_length=500, null=true)     talk6 = models.charfield(max_length=500, null=true)     talk7 = models.charfield(max_length=500, null=true)     talk8 = models.charfield(max_length=500, null=true)     characteristic1 = models.charfield(max_length=500,null=true)     characteristic2 = models.charfield(max_length=500, null=true)     characteristic3 = models.charfield(max_length=500, null=true)     characteristic4 = models.charfield(max_length=500, null=true)     characteristic5 = models.charfield(max_length=500, null=true)     characteristic6 = models.charfield(max_length=500, null=true)     characteristic7 = models.charfield(max_length=500, null=true)     characteristic8 = models.charfield(max_length=500, null=true) 

i wanna put null if list not have value.how should fix this?what should write it?

hi can handle list out of index error,you can add index null value

rows=sheet2.row_values(row_index) append_empty  = ['','','','',''] #here mention how many empty index need rows = rows + append_empty #goes update logic 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -