Removing duplicate values within records

983
6
Jump to solution
05-15-2019 08:31 AM
anTonialcaraz
Occasional Contributor II

Hi,

I got a sample script from an older post but I cannot get it working.

I'm trying to remove duplicate values within records in a FC.

For example, in the first row above, I need to get rid of one "63350" and one "70169" keeping the ";" as delimiters.

import arcpy


InputFC = r"H:\PROGRAMMES\10_OTHER_PROJECTS\24_ATTRIBUTION_CHECKS\From_RTJ.gdb\REFID_sample_1_1"
InputField = "REF_ID"

cursor = arcpy.da.UpdateCursor(InputFC, ["REF_ID"])


for row in cursor:

    strList = row.Value(InputField).split("; ")
    strSet = set(strList)

    str = "; "
    conStr = str.join(strSet)

    row.setValue(InputField, conStr)

    cur.updateRow(row)

del row
del cursor

I'm getting an error related to line 12: AttributeError: 'list' object has no attribute 'Value'

Any help would be greatly appreciated.

Thanks

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I revisited the documentation, and you are correct, i.e., row.Value is incorrect.  Correct syntax for the old cursors would involve getValue and setValue, not just Value.  So it appears to be mixing syntax and using incorrect syntax.

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

strList = row[0].split("; ")

doesn't work?  An not directly related example that splits on spaces IF the row isn't empty.

in_fc0 = r"Some_featureclass_in_a_spaceless_path"
cursor = arcpy.da.UpdateCursor(in_fc0, ["Text_1"])
for row in cursor:
    if row[0] is not None:
        strList = row[0].split(" ")
    print(strList)
    
['C', 'not', 'null']
['C', 'not', 'null']
['E_no_space']‍‍‍‍‍‍‍‍
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You are mixing the syntax between the legacy cursors and the newer Data Access cursors, that is why you are getting the specific error.  Review UpdateCursor—Help | ArcGIS for Desktop  for proper syntax.

DanPatterson_Retired
MVP Emeritus

Accessing data using cursors—ArcPy Get Started | ArcGIS Desktop 

actually both are supported

import arcpy

cursor = arcpy.da.SearchCursor(fc, ['fieldA', 'fieldB'])
for row in cursor:
    print(row)

#Search and update cursors also support with statements.

import arcpy

with arcpy.da.SearchCursor(fc, ['fieldA', 'fieldB']) as cursor:
    for row in cursor:
        print(row)
JoshuaBixby
MVP Esteemed Contributor

The issue wasn't about using the with, but how the OP was trying to retrieve values from the cursor:

cursor = arcpy.da.UpdateCursor(InputFC, ["REF_ID"])  # Created DA cursor
for row in cursor:
    strList = row.Value(InputField).split("; ")      # Used old cursor syntax
0 Kudos
DanPatterson_Retired
MVP Emeritus

Ahhhh, that is so old, I just thought it was an error Relic of ArcMap?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I revisited the documentation, and you are correct, i.e., row.Value is incorrect.  Correct syntax for the old cursors would involve getValue and setValue, not just Value.  So it appears to be mixing syntax and using incorrect syntax.