# Performing a Python scripted tool based on whether a field is populated or not

My question is if I'm running the ArcMap Dissolve tool through a Python script, how can I tell it to check for attributes in one field, and if that field is empty, to perform the tool on another field.

To be more specific, I would like the Dissolve tool to check for route numbers in a field called [rt_shrt_nm] (which are provided when the route name is a numerical value). But if that field is empty (which is sometimes is if the field has textual route names) to perform the tool on the [rt_long_nm] field instead.

This is what I'm working with now, just to give you a better idea, even though I know its probably wildly wrong:

if [rt_shrt_nm] is null:
arcpy.Dissolve_management(OutShapesFCname, outGDB, ["rt_long_nm"], "", "", "")
elif:
arcpy.Dissolve_management(OutShapesFCname, outGDB, ["rt_shrt_nm"], "", "", "")

-
But, are we talking about checking if all the layer is empty in that field, or you want to check each feature separately? Also, you have to use if-else statement, probably if you use if-elif, you will get Error (but not sure about this) – Óscar Martínez Aug 21 '14 at 21:25

I would use a search cursor to find out if there are any rows in a table with empty string (i.e., "").

fc = r"C:\ArcGIS\Default.gdb\Parcels"

with arcpy.da.SearchCursor(fc,"RouteName") as scur:
for field in scur:
if field[0] == "":
#dissolve on one field
else:
#dissolve on another field

-
Worked like a charm! Thank you very much Alex! P.s. really enjoyed reading your answer on "Do you need a Master's for a GIS career", as a student one semester short of my Bachelor's, it was extremely insightful – Eric Aug 21 '14 at 21:29
@Eric, glad it worked for you :) also happy my other answers are useful ;) – Alex Tereshenkov Aug 22 '14 at 5:41

I would generate a list from the rt_long_nm field with a list comprehension and use that for the logic check. The list comprehension searches for all values except for None and adds those to a list. If the length of the list equals 0, perform some action, else perform another action.

import arcpy

OutShapesFCname = r'C:\path\to\fc'

vals = [row[0] for row in arcpy.da.SearchCursor(OutShapesFCname, ["rt_long_nm"]) if row[0] is not None]

if len(vals) == 0:
arcpy.Dissolve_management(OutShapesFCname, outGDB, ["rt_long_nm"], "", "", "")
else:
arcpy.Dissolve_management(OutShapesFCname, outGDB, ["rt_shrt_nm"], "", "", "")

-