<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to print out records from Search Cursor  in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242408#M18850</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#import arcpy module&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True &lt;/P&gt;&lt;P&gt;# I am setting the work path &lt;/P&gt;&lt;P&gt;env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'&lt;/P&gt;&lt;P&gt;#I will set the variable for the schools shapefile&lt;/P&gt;&lt;P&gt;schools = 'Schools'&lt;/P&gt;&lt;P&gt;#create search cursor to loop thru schools. use where clause to return schools&lt;/P&gt;&lt;P&gt;#that are high schools. Use loops&lt;/P&gt;&lt;P&gt;#print out all the highschool names&lt;/P&gt;&lt;P&gt;#print total no. of high school records&lt;/P&gt;&lt;P&gt;sr = arcpy.SpatialReference(4326) &lt;/P&gt;&lt;P&gt;#I will create a search cursor for the schools shapefile&lt;/P&gt;&lt;P&gt;field = "NAME"&lt;/P&gt;&lt;P&gt;exp1 = '"FACILITY" = \'HIGH SCHOOL\''&lt;/P&gt;&lt;P&gt;cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#I have now printed out all of the names of the facilities that are high schools&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(field))&lt;/P&gt;&lt;P&gt;#this code above was successful &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#I am attempting to print out the number of facilities that are highschools&amp;nbsp; but this is wrong because it prints out the total no. of records and not the ones I queried above. &lt;/P&gt;&lt;P&gt;#while cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #count = arcpy.GetCount_management(env+schools+'.shp')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #result = int(count.getOutput(0)) &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #print result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #break &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 19 Aug 2015 01:18:33 GMT</pubDate>
    <dc:creator>fumaniwada1</dc:creator>
    <dc:date>2015-08-19T01:18:33Z</dc:date>
    <item>
      <title>How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242408#M18850</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#import arcpy module&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True &lt;/P&gt;&lt;P&gt;# I am setting the work path &lt;/P&gt;&lt;P&gt;env = 'S:\\376\\Summer15-2\\ahutche1\\lab07_data\\SectionB\\'&lt;/P&gt;&lt;P&gt;#I will set the variable for the schools shapefile&lt;/P&gt;&lt;P&gt;schools = 'Schools'&lt;/P&gt;&lt;P&gt;#create search cursor to loop thru schools. use where clause to return schools&lt;/P&gt;&lt;P&gt;#that are high schools. Use loops&lt;/P&gt;&lt;P&gt;#print out all the highschool names&lt;/P&gt;&lt;P&gt;#print total no. of high school records&lt;/P&gt;&lt;P&gt;sr = arcpy.SpatialReference(4326) &lt;/P&gt;&lt;P&gt;#I will create a search cursor for the schools shapefile&lt;/P&gt;&lt;P&gt;field = "NAME"&lt;/P&gt;&lt;P&gt;exp1 = '"FACILITY" = \'HIGH SCHOOL\''&lt;/P&gt;&lt;P&gt;cursor = arcpy.SearchCursor(env+schools+'.shp',exp1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#I have now printed out all of the names of the facilities that are high schools&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(field))&lt;/P&gt;&lt;P&gt;#this code above was successful &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#I am attempting to print out the number of facilities that are highschools&amp;nbsp; but this is wrong because it prints out the total no. of records and not the ones I queried above. &lt;/P&gt;&lt;P&gt;#while cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #count = arcpy.GetCount_management(env+schools+'.shp')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #result = int(count.getOutput(0)) &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #print result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #break &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 01:18:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242408#M18850</guid>
      <dc:creator>fumaniwada1</dc:creator>
      <dc:date>2015-08-19T01:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242409#M18851</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#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&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # defining a counter variable


#I have now printed out all of the names of the facilities that are high schools
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row.getValue(field))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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&amp;nbsp; but this is wrong because it prints out the total no. of records and not the ones I queried above. 
#while cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #count = arcpy.GetCount_management(env+schools+'.shp')
&amp;nbsp;&amp;nbsp;&amp;nbsp; #result = int(count.getOutput(0)) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print result
&amp;nbsp;&amp;nbsp;&amp;nbsp; #break &lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; (look for the lines I add "cntHS"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, please read &lt;A href="https://community.esri.com/migration-blogpost/1070" target="_blank"&gt;Posting Code blocks in the new GeoNet&lt;/A&gt;​&lt;/P&gt;&lt;P&gt;Makes it easier for others to read your code in GeoNet.&amp;nbsp; Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:08:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242409#M18851</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-11T12:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242410#M18852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sounds to me like a duplicate from this thread: &lt;A href="https://community.esri.com/thread/163911"&gt;How to correct SQL expression for Search Cursor in Arcpy&lt;/A&gt; in which the answer to your questions has already been provided.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:13:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242410#M18852</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-08-19T02:13:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242411#M18853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I added the code you suggested and for some reason it printed out each high school like this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*snippet* &lt;/P&gt;&lt;P&gt;JOHN B CONNALLY&lt;/P&gt;&lt;P&gt;There are 26 highschools.&lt;/P&gt;&lt;P&gt;OPPORTUNITY CENTER&lt;/P&gt;&lt;P&gt;There are 27 highschools.&lt;/P&gt;&lt;P&gt;MANOR NEW TECH&lt;/P&gt;&lt;P&gt;There are 28 highschools.&lt;/P&gt;&lt;P&gt;OPPORTUNITY CENTER HS &amp;amp; MS&lt;/P&gt;&lt;P&gt;There are 29 highschools.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;##############&lt;/P&gt;&lt;P&gt;Is there a way to just have it print out 1 sentence saying there are 29 high schools? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:15:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242411#M18853</guid>
      <dc:creator>fumaniwada1</dc:creator>
      <dc:date>2015-08-19T02:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242412#M18854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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! &lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:18:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242412#M18854</guid>
      <dc:creator>fumaniwada1</dc:creator>
      <dc:date>2015-08-19T02:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242413#M18855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That happened since you indented the code on line 26... The code that &lt;A href="https://community.esri.com/migrated-users/2691"&gt;Rebecca Strauch&lt;/A&gt; posted will print the number of high schools only once...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:23:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242413#M18855</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-08-19T02:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out records from Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242414#M18856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;ahh thanks since this thread is too similar to the other one I'll delete this one. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:35:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-print-out-records-from-search-cursor/m-p/242414#M18856</guid>
      <dc:creator>fumaniwada1</dc:creator>
      <dc:date>2015-08-19T02:35:15Z</dc:date>
    </item>
  </channel>
</rss>

