Skip to main content
 首页 » 编程设计

python之使用python在excel中删除具有某些列组合的重复行

2025年05月04日13qq78292959

我有一个读取 excel 文档的 python 程序。我只需要允许某些列组合的首次出现。 例如:

    A     |  B 
  ------------- 
  1.  200 | 201    
  2.  200 | 202 
  3.  200 | 201 
  4.  200 | 203 
  5.  201 | 201 
  6.  201 | 202 
  ............. 

我想删除/跳过发现重复项的第三行并将其写入 CSV 文件。 这是我到目前为止一直在尝试的功能。但它不起作用。

def validateExcel(filename): 
   xls=xlrd.open_workbook(filename)   
   setcount = 0 
   column = 0 
   count = 0 
   # sheetcount = 0 
   for sheet in xls.sheets(): 
       header="" 
       # sheetcount = sheetcount + 1 
       number_of_rows = sheet.nrows 
       number_of_columns = sheet.ncols 
       sheetname = sheet.name           
       mylist = [] 
       for row in range (1, number_of_rows):   
           mylist = [] 
           for col in range(0, 2):       
               mylist.append(sheet.cell_value(row, col)) 
 
           print mylist 
 
           myset = set(mylist) 
 
           print myset 

请您参考如下方法:

它对我有用:在 python 2.7 中

def validateExcel(filename): 
   xls=xlrd.open_workbook(filename)   
   setcount = 0 
   column = 0 
   count = 0 
   # sheetcount = 0 
   for sheet in xls.sheets(): 
       header="" 
       # sheetcount = sheetcount + 1 
       number_of_rows = sheet.nrows 
       number_of_columns = sheet.ncols 
       sheetname = sheet.name           
       mylist = [] 
       for row in range(1, number_of_rows):   
            mylist.append((sheet.cell_value(row, 0), sheet.cell_value(row, 1))) 
       myset = sorted(set(mylist), key=mylist.index) 
       return myset