选择随机行(Excel):列表分配索引超出范围

Selecting random rows (Excel): list assignment index out of range

我正在尝试从 Excel 文件中 select 408 行随机并将它们另存为新的 Excel 文件,这样一行只能是 selected 一次,但我收到一个我不理解的 IndexError。

这是我的代码:

# opening the source excel file
wb1 = xl.load_workbook('Test_low.xlsx')
ws1 = wb1.worksheets[0]
  
# opening the destination excel file 
wb2 = xl.load_workbook('Parsed.xlsx')
ws2 = wb2.active

#number of rows
lastrow = ws1.max_row

#number of columns
lastcol = ws1.max_column

#select 408 random rows
i = 0
items = [*range(1,lastrow+1)]
while i <= 407:
    for j in range(1, lastcol):
        selected = randint(1,lastrow)
        #   read row value from source excel file
        c = ws1.cell(row = selected,column = j)
        # write the read value to destination excel file
        ws2.cell(row = i+1,column = j).value = c.value
        del items[selected]
        i =+ 1
        
# save destination excel file
wb2.save(str('Parsed.xlxs'))

这是引发的错误:

  File "C:\(code file)", line 35, in <module>
    del items[selected]

IndexError: list assignment index out of range

谁能帮我理解“selected”是如何超出范围的?我仍然是 Python 的初学者,所以我有点迷茫。 谢谢!

当您从要从中删除项目的列表中编制索引时,它超出了范围。相反,只需获取一个随机的索引样本,这样我们就不需要删除重复项,然后只需遍历采样行的所有列并将它们附加到新的 excel sheet。 (我不确定你为什么要在所有范围中添加一个,所以我删除了它)。

items = [*range(lastrow)]
selectedRows = random.sample(items, 408)

for i in selectedRows:
    for j in range(lastcol):
        #   read row value from source excel file
        c = ws1.cell(row = i,column = j)
        # write the read value to destination excel file
        ws2.cell(row = i,column = j).value = c.value