Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Unable to delete duplicates in excel with Python
#1
I am trying to delete duplicates but the job just finishes with an exit code 0 and does not delete any duplicates.

I have attempted to do this with openpyxl for an excel as well as other methods (including csv though this deleted rows excessively).

The duplicates for the data always exist in Column F and I am desiring to delete the entire row B-I

Any ideas?

import openpyxl
wb1 = openpyxl.load_workbook('C:/dwad/SWWA.xlsx')
ws1 = wb1.active # keep naming convention consistent

values = []
for i in range(2,ws1.max_row+1):
  if ws1.cell(row=i,column=1).value in values:
    #pass
  #else:
    values.append(ws1.cell(row=i,column=1).value)

for value in values:
  ws1.append([value])

CSV:
with open('1.csv','r') as in_file, open('2.csv','w') as out_file:
    seen = set() # set for fast O(1) amortized lookup
    for line in in_file:
        if line not in seen: 
            seen.add(line)
            out_file.write(line)
İmage
Quote
#2
Trying to set it for a set range... As it's only getting one row so far.. Likely have to iterate...

wb1 = openpyxl.load_workbook('C:/adw.xlsx')
ws = wb1.active
wb2 = openpyxl.Workbook()
ws2 = wb2.active

ws1 = wb1.active  # keep naming convention consistent

column = ['A:B']
values = []
for row in range(1, ws1.max_row):
    if ws1.cell(row=row, column=row).value in values:
        pass  # if already in list do nothing
    else:
        values.append(ws1.cell(row=row, column=row).value)

directory = 'C:/dwadwaddwad.csv'
with open(directory, 'a', newline='', encoding="utf-8") as outfile:
    for value in values:
        ws2.append([value])
        print(value)
wb2.save('C:/dwadwadaw.xlsx')

I don't think openpyxl supports this. It can be done with line in Vba though https://msdn.microsoft.com/en-us/vba/exc...thod-excel
Quote
#3
Solved. I use about 400 lines of code. But it does the job if you just iterate through the same file 12 times.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  delete a Python object that matches an atribute portafreak 2 217 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Unable to write to excel - Using openpyxl module starstud 2 285 Feb-05-2020, 03:53 AM
Last Post: starstud
  how do i pass duplicates in my range iterator? pseudo 3 254 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  Deleting duplicates in tuples Den 2 398 Dec-14-2019, 10:32 PM
Last Post: ichabod801
  Not Able To Delete First Node From Python Linked List ribena1980 8 798 Mar-05-2019, 03:14 PM
Last Post: ichabod801
  How to remove duplicates basis keys of a csv file Prince_Bhatia 2 766 Aug-09-2018, 10:57 AM
Last Post: Prince_Bhatia
  Find duplicate images and delete them using python and openCVq Prince_Bhatia 2 5,970 Dec-05-2017, 05:52 PM
Last Post: Prince_Bhatia

Forum Jump:


Users browsing this thread: 1 Guest(s)