<?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 Re: Doing multiple queries with a loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741111#M57301</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for your reply.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried your code in my script "test_rte.py" (adapting it by using arcgisscripting instead of acrpy) but I have an error message :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp; File "C:\_basedata_arcgis\test_rte.PY", line 14, in &amp;lt;module&amp;gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: # loop through each feature&lt;BR /&gt;TypeError: 'geoprocessing cursor object' object is not iterable&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What could I do to correct this ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 18 Jan 2012 19:33:11 GMT</pubDate>
    <dc:creator>julienhubert</dc:creator>
    <dc:date>2012-01-18T19:33:11Z</dc:date>
    <item>
      <title>Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741109#M57299</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello everyone !&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I work on ArcGIS 9.3 and I'd want to execute many queries in a row with Python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Actually I have a layer, called "lines" that contains all the bus lines of a city. And my question is : how to save each line in a different layer ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; I thought the best thing to do would be : &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- a query by attributes : field used : "blines". Characterizes the number of the lines (1, 2, 3 and so on)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- and then to save each query in a distinct layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A good solution would be a loop in order to make the computation faster but I don't really know how to do. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to make that script but I guess I'm very far from the right result :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Variables in local
lines_shp = "d:\\Travaux\\NantesMetropole\\lines.shp"

test = ["blines"]
while 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in test:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis(lines_shp, i_shp, i)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Query successful"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Error"
&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Have you got any ideas ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you !&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Julien&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 17:10:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741109#M57299</guid>
      <dc:creator>julienhubert</dc:creator>
      <dc:date>2012-01-18T17:10:35Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741110#M57300</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can adapt this to suit your needs:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

arcpy.env.overwriteOutput = True

fc = "d:/Travaux/NantesMetropole/lines.shp" # path to input feature class
field = "blines" # field name to get unique values
arcpy.MakeFeatureLayer_management(fc, "lyr") # make feature layer
rows = arcpy.SearchCursor(fc, "", "", "", field) # make cursor, sort by field

firsttime = 1 
prev = -1

for row in rows: # loop through each feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; current = row.getValue(field) # get current field value
&amp;nbsp;&amp;nbsp;&amp;nbsp; if firsttime == 1 or prev != current: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; prev = row.getValue(field) # set prev to current value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", field + " = " + str(row.mapnumber))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("lyr", "d:/Travaux/NantesMetropole/lines" + str(current) + ".shp") 
arcpy.Delete_management("lyr")
del row
del rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:32:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741110#M57300</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T07:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741111#M57301</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for your reply.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried your code in my script "test_rte.py" (adapting it by using arcgisscripting instead of acrpy) but I have an error message :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp; File "C:\_basedata_arcgis\test_rte.PY", line 14, in &amp;lt;module&amp;gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: # loop through each feature&lt;BR /&gt;TypeError: 'geoprocessing cursor object' object is not iterable&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What could I do to correct this ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 19:33:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741111#M57301</guid>
      <dc:creator>julienhubert</dc:creator>
      <dc:date>2012-01-18T19:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741112#M57302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Oh, sorry - didn't see you're on 9.3. Arcpy only works on 10. You can basically use the same script format, just substitute arcgisscripting for arcpy and create the geoprocessor object as you did above.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 19:43:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741112#M57302</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-01-18T19:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741113#M57303</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In arcgisscripting, I think you need to add the line&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;row = rows.next()
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;between creating the SearchCursor and the loop. You also need to add that line to the very end of the loop to advance the cursor. See &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=SearchCursor_method" rel="nofollow noopener noreferrer" target="_blank"&gt;this help page&lt;/A&gt;&lt;SPAN&gt; to see an example.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:32:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741113#M57303</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T07:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741114#M57304</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yeah it's right, I corrected it and it looks good....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... but another new error message :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", field + " = " + str(row.mapnumber))&lt;BR /&gt;RuntimeError: Row: Field mapnumber does not exist&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What exactly is the row.mapnumber function ? I took a look in both the python docs and the arcgis help but it's not explained..&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 20:00:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741114#M57304</guid>
      <dc:creator>julienhubert</dc:creator>
      <dc:date>2012-01-18T20:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741115#M57305</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry, "mapnumber" was the field I tested the script with (I thought I changed them all). You can change str(row.mapnumber) to str(current).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 20:09:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741115#M57305</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-01-18T20:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741116#M57306</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok thanks for row.mapnumber !&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So new try and there is an error on the execution of SelectLayerByAttribute :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", field + " = " + str(current))&lt;BR /&gt;ExecuteError: ERROR 000358: Invalid expression&lt;BR /&gt;Failed to execute (SelectLayerByAttribute).&lt;/STRONG&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 20:30:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741116#M57306</guid>
      <dc:creator>julienhubert</dc:creator>
      <dc:date>2012-01-18T20:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741117#M57307</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It works for me. Instead of: gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", field + " = " + str(current))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;expr = field + " = " + str(current)
gp.SelectLayerByAttribute("lyr", "NEW_SELECTION", expr)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:32:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741117#M57307</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T07:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741118#M57308</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, I tried many and many things including your last advice but it still does not work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't want to waste your time but could you write the entire code (since it works for you) ?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jan 2012 23:04:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741118#M57308</guid>
      <dc:creator>julienhubert</dc:creator>
      <dc:date>2012-01-18T23:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741119#M57309</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

arcpy.env.overwriteOutput = True

fc = "H:/GIS_Data/TEMP.gdb/points" # path to input feature class
field = "mapnumber" # field name to get unique values
arcpy.MakeFeatureLayer_management(fc, "lyr") # make feature layer
rows = arcpy.SearchCursor(fc, "", "", "", field) # make cursor, sort by field

firsttime = 1 
prev = -1

for row in rows: # loop through each feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; current = row.getValue(field) # get current field value
&amp;nbsp;&amp;nbsp;&amp;nbsp; if firsttime == 1 or prev != current: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; prev = row.getValue(field) # set prev to current value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", field + " = " + str(current)) 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("lyr", "H:/GIS_Data/TEMP.gdb/pt_" + str(current)) 
arcpy.Delete_management("lyr")
del row
del rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:32:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741119#M57309</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T07:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741120#M57310</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you want every feature exported as a seperate feature class, you would probably want to use the OBJECTID field (or some other unique ID field).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#DISCLAIMER: Untested code - just typed it on the fly - may not work "out of the box"
import arcpy
myFC = r"C:\temp\my_fc.shp"
oidFieldName = arcpy.Describe(myFC).OIDFieldName
searchRows = arcpy.SearchCursor(myFC)
for searchRow in searchRows:
&amp;nbsp;&amp;nbsp; oidFieldValue = searchRow.getValue(oidFieldName)
&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\output_" + str(oidFieldValue) + ".shp"
&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, oidFieldName + " = " + str(oidFielValue))
del searchRow, searchRows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:32:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741120#M57310</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-12T07:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Doing multiple queries with a loop</title>
      <link>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741121#M57311</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If you want every feature exported as a seperate feature class, you would probably want to use the OBJECTID field (or some other unique ID field).&lt;BR /&gt; &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;That assumes that each bus line is made of only one feature...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Jan 2012 17:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/doing-multiple-queries-with-a-loop/m-p/741121#M57311</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-01-19T17:48:22Z</dc:date>
    </item>
  </channel>
</rss>

