[GIS] Using Search Cursor and Messaging Total Count of Items from Field in Layer that was Searched

arcpycursor

My program works but it is not displaying a message of the total sheet count found in attribute table that the search cursor searched through. For Example: It goes through the mxd chosen by user finds the layer with the name Grid in it and then is supposed to count the rows in the field Sheet_ID. End result is to print a message of the count found. My code is below:

import arcpy, os, sys
from os import path as p
from arcpy import mapping as m

mxdList = arcpy.GetParameterAsText(0).split(';')


sheet_count = ()

for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    mxd = arcpy.mapping.MapDocument(mapDoc)
    for lyr in m.ListLayers(mxd, "*, Grid"):
        if lyr.description == "*, Grid":
        max_list = []
        rows = arcpy.SearchCursor(lyr)
        for row in rows:
            max_list.append(row.Sheet_ID)
        sheet_count = max(max_list)
        arcpy.AddMessage('Sheet count: %s'%sheet_count)
        print 'Sheet count: %s'%sheet_count
    else:
        arcpy.AddError('No Layers in %s match data source'%mapDoc)  

Best Answer

Your question states that you are looking for the "total count of items from field". Yet, you use the max statement and claim that this is what works. Unless the max value in that particular field does indeed match the number of rows, your output isn't for what you are looking.

If you just want the number of entries, it's a simple call to Get Count.

int(arcpy.GetCount_management(lyrfile).getOutput(0))

If that's not what you are looking for @gotchula has a post that will solve the issue efficiently, assuming you are running ArcMap 10.1, and have access to the data access module, da.

Other than that, there are syntax errors that others have pointed out. Indentation is critical in Python.