I have a number of columns in a number of tables withinh a FGDB where I need to extract the unique values for each column.
For Example: the values may be [1,2,2,2,3,4], and I am trying to return [1,2,3,4]
I could do this job a number of other ways in ARCGIS but I am trying to extend myself.
I have found a piece of python on the web that I think will do the job but I am struggling to get it to run (I keep getting an invalid syntax error as I keep getting the syntax error in line 3) this will no doubt be a really simple user error.
Code Snippet below
import arcpy
def unique_values(r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj'):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
This is the Error Message I get from sublime text:
File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 3
def unique_values(r'N:\GISProjects\Steve_Eastwood_Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj'):
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
Updates from Original Question
I have now updated my code with the answer provided by below but am recieving a secondary error.
New Code Snippet:
import arcpy
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
myValues = unique_values(r'N:\\GISProjects\\Landuse\\Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge' , 'LU_ALUMMaj')
print (myValues)
I am getting a new error message related to a runtime error
Traceback (most recent call last):
File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 7, in <module>
myValues = unique_values(r'N:\\GISProjects\\Steve_Eastwood_Landuse \Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge' , 'LU_ALUMMaj')
File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 4, in unique_values
with arcpy.da.SearchCursor(table, [field]) as cursor:
RuntimeError: cannot open 'N:\\GISProjects\\Steve_Eastwood_Landuse\\Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge'
[Finished in 8.0s with exit code 1]
I assume from the reading I have done that this relates to setting the env.workspace ?
env.workspace
I don't think. Try either taking off ther
before the path, or changing the\` to
` in the path (and leave ther
there). Does that Geodatabase exist?[1,2,2,2,3,4]
, are you trying to return[1,2,3,4]
. Please update the post to include this information.