【问题标题】:IndexError: list index out of range: I can uderstand why this error happensIndexError: list index out of range: 我可以理解为什么会发生这个错误
【发布时间】:2018-02-20 08:25:18
【问题描述】:

我收到一个错误 IndexError: list index out of range。 我想将字典数据放入模型(用户)。 我写了

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])

但是,每个列表的索引行数是不同的。行数是这样的

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

所以每个列表的索引数量不同,最大索引为 13。 models.py 是

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)

如果列表没有价值,我想放空。我应该如何解决这个问题?我应该写什么?

【问题讨论】:

    标签: python django excel


    【解决方案1】:

    与字典不同,没有安全的方法可以访问列表中可能不存在的索引。

    在您的示例中,我知道在某些行中您不会有索引 16、17 等...

    我建议你看看这个答案:https://stackoverflow.com/a/5125636/3620496

    在您的user3.update() 中,您可以将所有列表访问权限替换为safe_list_get(rows, index_you_want, 'Default Value you want')

    看看这些答案,都可以给出答案的一些要素:Getting a default value on index out of range in Python

    【讨论】:

      【解决方案2】:

      你可以处理这样的列表超出索引错误,你可以添加额外的空值索引

      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
      

      【讨论】:

        猜你喜欢
        • 2015-02-11
        • 1970-01-01
        • 2022-11-28
        • 2020-10-27
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        相关资源
        最近更新 更多