在Openpyxl中查找重复行的索引

时间:2018-05-31 12:31:45

标签: python excel openpyxl

我想找到excel文件中所有重复行的索引,并将它们添加到稍后将处理的列表中。

unwantedRows = []
Row = []
item = ""

for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
  for cell in row:
    if cell.value:
      item += cell.value
  if item in Row:
    unwantedRows.append(index)
  else:
    Row.append(item)

然而,这无效。它仅索引完全为空的行。我该如何解决?

2 个答案:

答案 0 :(得分:0)

unwantedRows = []
Rows = []

for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
  sublist = []
  for cell in row:
    sublist.append(cell.value)

  if sublist not in Rows:
    Rows.append((sublist))
  else:
    unwantedRows.append(index)

答案 1 :(得分:0)

没有元组:

row_numbers_to_delete = []
rows_to_keep = []
for row in ws.rows:
    working_list = []
    for cell in row:
        working_list.append(cell.value)
    if working_list not in rows_to_keep:
        rows_to_keep.append(working_list)
    else:
        row_numbers_to_delete.append(cell.row)
for row in row_numbers_to_delete:
    ws.delete_rows(
        idx=row,
        amount=1
    )