Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)  
share|improve this question
    
Are you running this from ArcGIS or command line? Have you tried running it from the command window in ArcGIS? –  Jamie Mar 7 '13 at 20:23
    
I am running it in ArcGIS through a script tool with the parameters for the selection of the mxd. –  Michelle Couden Mar 7 '13 at 21:06
1  
There is a syntax error here. Indentation is part of the syntax in Python and you have forgotten a level of indentation after the line if lyr.description == "*, Grid":. Unless you just messed up with the copy/paste of the code into your question. –  blah238 Mar 8 '13 at 2:57
    
Also the same line doesn't make much sense to me. Are you sure it's doing what you expect? Have you stepped through this in a debugger? See my answer to your other question. –  blah238 Mar 8 '13 at 2:58

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.

share|improve this answer

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
share|improve this answer

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/

share|improve this answer
1  
If 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 Mar 8 '13 at 3:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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