Select to view content in your preferred language

How to print out records from Search Cursor

4258
6
08-18-2015 06:18 PM
fumaniwada1
Deactivated User

I have a schools shapefile with different facilities. I created a search cursor to only select records where the field "name" is equal to high school. Then I was to use a for loop to print out all the names of the different high schools. I managed to complete this successfully;however, I must also print out the no. of high schools that I just queried in the search cursor. So for instance I managed to use the search cursor to print out 35 high schools (or atleast their names). How would I use the search cursor to print out the no. of records or no. of highschools that I have just printed out. I can't use the selectlayer only search cursor. Here's my code below.

#import arcpy module

import arcpy

from arcpy import env

arcpy.env.overwriteOutput = True

# I am setting the work path

env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'

#I will set the variable for the schools shapefile

schools = 'Schools'

#create search cursor to loop thru schools. use where clause to return schools

#that are high schools. Use loops

#print out all the highschool names

#print total no. of high school records

sr = arcpy.SpatialReference(4326)

#I will create a search cursor for the schools shapefile

field = "NAME"

exp1 = '"FACILITY" = \'HIGH SCHOOL\''

cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)

#I have now printed out all of the names of the facilities that are high schools

for row in cursor:

    print(row.getValue(field))

#this code above was successful

#I am attempting to print out the number of facilities that are highschools  but this is wrong because it prints out the total no. of records and not the ones I queried above.

#while cursor:

    #count = arcpy.GetCount_management(env+schools+'.shp')

    #result = int(count.getOutput(0))

    #print result

    #break

0 Kudos
6 Replies
RebeccaStrauch__GISP
MVP Emeritus
#import arcpy module
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True 
# I am setting the work path 
env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'
#I will set the variable for the schools shapefile
schools = 'Schools'
#create search cursor to loop thru schools. use where clause to return schools
#that are high schools. Use loops
#print out all the highschool names
#print total no. of high school records
sr = arcpy.SpatialReference(4326) 
#I will create a search cursor for the schools shapefile
field = "NAME"
exp1 = '"FACILITY" = \'HIGH SCHOOL\''
cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)
cntHS = 0     # defining a counter variable


#I have now printed out all of the names of the facilities that are high schools
for row in cursor:
    print(row.getValue(field))
    cntHS += 1
#this code above was successful 
print("Number of High Schools: {0}".format(cntHS))


#I am attempting to print out the number of facilities that are highschools  but this is wrong because it prints out the total no. of records and not the ones I queried above. 
#while cursor:
    #count = arcpy.GetCount_management(env+schools+'.shp')
    #result = int(count.getOutput(0)) 
    #print result
    #break 

How about adding a simple cnt variable with a counter inside your cursor as shown above.....I'm assuming your notes about the rest working are correct.  (look for the lines I add "cntHS"

Also, please read Posting Code blocks in the new GeoNet

Makes it easier for others to read your code in GeoNet.  Thanks

fumaniwada1
Deactivated User

I added the code you suggested and for some reason it printed out each high school like this

*snippet*

JOHN B CONNALLY

There are 26 highschools.

OPPORTUNITY CENTER

There are 27 highschools.

MANOR NEW TECH

There are 28 highschools.

OPPORTUNITY CENTER HS & MS

There are 29 highschools.

##############

Is there a way to just have it print out 1 sentence saying there are 29 high schools?

0 Kudos
XanderBakker
Esri Esteemed Contributor

That happened since you indented the code on line 26... The code that Rebecca Strauch posted will print the number of high schools only once...

fumaniwada1
Deactivated User

ahh thanks since this thread is too similar to the other one I'll delete this one.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Sounds to me like a duplicate from this thread: How to correct SQL expression for Search Cursor in Arcpy in which the answer to your questions has already been provided.

fumaniwada1
Deactivated User

actually I ended up getting the answer on my own and my small script change was a lot faster and required less code. Thanks though I forgot to check the old post. I figured out the python scripting part but I was using get count management in the oddest way!

I'm new this part of esri. I never knew it existed except for google. Now I've printed out the schools, but I'd rather have the total no. of records print out once.

0 Kudos