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)
3 Answers 3
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.
Look at replacing this
'Sheet count: %s'%sheet_count
With this
'Sheet count: {}'.format(sheet_count)
It is the modern way, and it's much more robust. For doc see http://docs.python.org/2/library/string.html#format-string-syntax
Also, walking through all records, then building a list of all sheet_ID then asking for max is very inefficient.
This section of code
max_list = []
rows = arcpy.SearchCursor(lyr)
for row in rows:
max_list.append(row.Sheet_ID)
sheet_count = max(max_list)
replace it with this
with arcpy.da.SearchCursor(lyr, "Sheet_ID",
sql_clause=(None, "ORDER BY Sheet_ID DESC")) as cur:
for row in cur:
# just get the first value (largest) and quit
sheet_count = row[0]
break
I believe you need to print the messages that you have added. So you need to call arcpy.GetMessages() then print.
print arcpy.GetMessages()
or you can do this:
# Return the resulting messages as script tool output messages
#
x = 0
while x < arcpy.MessageCount:
arcpy.AddReturnMessage(x)
x = x + 1
All of this is in the help here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Writing_messages_in_script_tools/00150000000p000000/
-
1If run from a script tool,
arcpy.AddMessage
is the correct function to use to display messages in the tool status window and the geoprocessing results.blah238– blah2382013年03月08日 03:00:48 +00:00Commented Mar 8, 2013 at 3:00
if lyr.description == "*, Grid":
. Unless you just messed up with the copy/paste of the code into your question.