2

I got an error IndexError: list index out of range. I wanna put dictionary data to the model(User) . I wrote

book2 = xlrd.open_workbook('./data/excel1.xlsx')
sheet2 = book2.sheet_by_index(0)
for 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 is different from each list.rows is like

['001200cd3eF', 'Tom', 'Hi', 'greeting', 'Goodmorning', 'greeting', 'Bye' 'say good‐bye', '', '', '']['007700ab7Ws', 'Blear', 'How are you', 'greeting', 'Thx', 'Thanks', '', '', '']

so each list's the number of index is different,max index is 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 a list does not have value.How should I fix this?What should I write it?

user8504021
  • 303
  • 5
  • 23

2 Answers2

1

Unlike with dictionaries, there is no safe way to access an index that might not exist in your list.

In your example, I understand that in some rows you won't have indexes 16, 17, etc...

I suggest you to look at this answer : https://stackoverflow.com/a/5125636/3620496

In your user3.update() you can replace all list access by safe_list_get(rows, index_you_want, 'Default Value you want')

Have a look at these answers, they all can give some elements of answer: Getting a default value on index out of range in Python

MiniYuuzhan
  • 108
  • 3
  • 14
1

Hi can handle like that list out of index error,you can add extra index with null value

rows=sheet2.row_values(row_index)
append_empty  = ['','','','',''] #here mention how many empty index as you need
rows = rows + append_empty
#goes to your update logic
Robert
  • 3,373
  • 1
  • 18
  • 34