-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteDuplicates.py
More file actions
69 lines (55 loc) · 1.96 KB
/
DeleteDuplicates.py
File metadata and controls
69 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''****************************************************************
DeleteDuplicate.py
Author(s): Anders Olson
Usage: Run script using python IDE or similar
Description:
Script traverses a selected attribute field of input dataset
to find duplicates. Only one record per a duplicate can exist;
script deletes one of the duplicates.
****************************************************************'''
import arcpy
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Configure logger
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
def outputMessage(msg):
print(msg)
arcpy.AddMessage(msg)
def outputError(msg):
print(msg)
arcpy.AddError(msg)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Define environment and messaging
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
# Set the overwriteOutput ON
arcpy.gp.overwriteOutput = True
# Set workspace to be in memory for faster run time
arcpy.env.workspace = "in_memory"
outputMessage("Workspace is: {}".format(arcpy.env.workspace))
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Define variables
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
# Input dataset to find and remove duplicates
inData = r'C:\Users\andolson\Documents\WORKING\TEMP\SampleData.shp'
# List for tracking duplicate IDs
idList = []
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#================================#
# Call Functions
#================================#
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#Populate a list of attributes from desired input field and delete any duplicates, but only one duplicate.
with arcpy.da.UpdateCursor(inData, ['FIPSSTCO']) as cursor:
for row in cursor:
if row[0] in idList:
cursor.deleteRow()
elif row[0] not in idList:
idList.append(row[0])
else:
outputMessage("Error building list...")
outputMessage(len(idList))