3

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"], "", "", "")
0

2 Answers 2

3

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
0
1

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"], "", "", "")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.