<?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: searchcursor on layer using text field and query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380810#M30022</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Damian,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kim already suggested a valid syntax for your where clause, but I would like to add something for you to consider. Since the syntax of the where clause depends on the dataset, it might be better to write code that is independent of the type of data you are querying. For this purpose arcpy provides the AddFieldDelimiters functionality. Have a look at the code below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fldname = "FIRECODE"
lst_values = ["ARC", "CEA", "DAB", "MGD"]
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values))

cur = arcpy.SearchCursor(lyr, where)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In this case I have the field name defined as a variable "fldname" and I also have a list of values to match it to ("lst_values"). I create a variable "where" containing the where clause and pass that variable in the search cursor (BTW if you have access to 10.1 you should try the arcpy.da.SearchCursor which is much faster, but uses a different syntax).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let's take the where clause construction apart to explain what happens:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The AddFieldDelimiter adds the quotes, square brackets or nothing depending on the data source:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print arcpy.AddFieldDelimiters(lyr, fldname)
# &amp;gt;&amp;gt; "FIRECODE"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you use the join method on a string a trow in a list, it will return the list of items where the string is placed between each item:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print "','".join(lst_values)
# &amp;gt;&amp;gt; ARC','CEA','DAB','MGD&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you use the format on a string you can replace the {0}, {1} by values you pass in, allowing better readability. In this case the list is enclosed by curved brackets and single quotes: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print "('{0}')".format("','".join(lst_values))
# &amp;gt;&amp;gt; ('ARC','CEA','DAB','MGD')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Putting it all together gives:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values))
print where
# &amp;gt;&amp;gt; "FIRECODE" in ('ARC','CEA','DAB','MGD')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:33:41 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-12-11T17:33:41Z</dc:date>
    <item>
      <title>searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380808#M30020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, I'm trying to use a SearchCursor to loop through specific records in a layer in an ArcMap project.&amp;nbsp; I would like loop through specific records based on a text field in the layer but I'm having trouble figuring out the syntax. Can anyone help please? Code below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Set the variables mxd = arcpy.mapping.MapDocument(r"\\working\BioFire.mxd") df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] lyr = arcpy.mapping.ListLayers(mxd,"Fire",df)[0]&amp;nbsp; # Loop through specified records in layer&amp;nbsp; ### I CAN'T FIGURE OUT THE CODE FOR THIS NEXT BIT cur = arcpy.SearchCursor(lyr, '"FIRECODE" in ("ARC", "CEA", "DAB", "MGD")')&amp;nbsp;&amp;nbsp;&amp;nbsp; ### IF I USE THE LINE BELOW INSTEAD IT WORKS FINE BUT LOOPS THROUGH ALL RECORDS WHICH I DON'T WANT #cur = arcpy.SearchCursor(lyr)&amp;nbsp; . . . . &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers, Damian&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 02:09:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380808#M30020</guid>
      <dc:creator>DamianMilne</dc:creator>
      <dc:date>2014-03-12T02:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380809#M30021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your SQL query has the quotes mixed up. SQL uses single quotes only for strings, and there is a general kludge to surround invalid field names with double quotes. If the field name has no spaces or punctuation, speclal characters, does not start with a number or an other indecencies in a field name then you can forget the double quotes. This is not the official advice of course, and if you do not have control of field names you may have to add double quotes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;SQLexpression = "FIRECODE in ('ARC', 'CEA', 'DAB', 'MGD')" cur = arcpy.SearchCursor(lyr,sQLexpression)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Similarly if you really want to have double quotes in the SQL query (why?) instead of escaping them with clumsy escape characters (\) just surround the whole expression with triple quotes of either type. Spaces added for clarity on the triples.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;SQLexpression = ''' "FIRECODE" in ('ARC', 'CEA', 'DAB', 'MGD') ''' cur = arcpy.SearchCursor(lyr,sQLexpression)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 07:32:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380809#M30021</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2014-03-12T07:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380810#M30022</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Damian,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kim already suggested a valid syntax for your where clause, but I would like to add something for you to consider. Since the syntax of the where clause depends on the dataset, it might be better to write code that is independent of the type of data you are querying. For this purpose arcpy provides the AddFieldDelimiters functionality. Have a look at the code below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fldname = "FIRECODE"
lst_values = ["ARC", "CEA", "DAB", "MGD"]
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values))

cur = arcpy.SearchCursor(lyr, where)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In this case I have the field name defined as a variable "fldname" and I also have a list of values to match it to ("lst_values"). I create a variable "where" containing the where clause and pass that variable in the search cursor (BTW if you have access to 10.1 you should try the arcpy.da.SearchCursor which is much faster, but uses a different syntax).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let's take the where clause construction apart to explain what happens:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The AddFieldDelimiter adds the quotes, square brackets or nothing depending on the data source:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print arcpy.AddFieldDelimiters(lyr, fldname)
# &amp;gt;&amp;gt; "FIRECODE"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you use the join method on a string a trow in a list, it will return the list of items where the string is placed between each item:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print "','".join(lst_values)
# &amp;gt;&amp;gt; ARC','CEA','DAB','MGD&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you use the format on a string you can replace the {0}, {1} by values you pass in, allowing better readability. In this case the list is enclosed by curved brackets and single quotes: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print "('{0}')".format("','".join(lst_values))
# &amp;gt;&amp;gt; ('ARC','CEA','DAB','MGD')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Putting it all together gives:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values))
print where
# &amp;gt;&amp;gt; "FIRECODE" in ('ARC','CEA','DAB','MGD')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:33:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380810#M30022</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T17:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380811#M30023</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Dear Kim and Xander, thank you very much for your responses, both worked a treat. I wish the ESRI online help was that clear and succinct.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander, I'm afraid you've started something. I do have 10.1 (10.2 actually), and I tried using arcpy.da.SearchCursor as you suggested. I followed the esri online help and examples as best I could and ended up with this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
datasetname = lyr.dataSource&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #this is a shapefile
fldname = "FIRECODE"
lst_values = ["ARC", "CEA", "DAB", "MGD"]
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values))
with arcpy.da.SearchCursor (datasetname,fldname,where) as cur:
&amp;nbsp; for eachrecord in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; regcode = eachrecord.getValue(fldname)
.
.
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At this point I get the error message "AttributeError: 'tuple' object has no attribute 'getValue'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions? Cheers, Damian&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:33:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380811#M30023</guid>
      <dc:creator>DamianMilne</dc:creator>
      <dc:date>2021-12-11T17:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380812#M30024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Dear Kim and Xander, thank you very much for your responses, both worked a treat. I wish the ESRI online help was that clear and succinct.&lt;BR /&gt;&lt;BR /&gt;Xander, I'm afraid you've started something. I do have 10.1 (10.2 actually), and I tried using arcpy.da.SearchCursor as you suggested. I followed the esri online help and examples as best I could and ended up with this:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
datasetname = lyr.dataSource&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #this is a shapefile
fldname = "FIRECODE"
lst_values = ["ARC", "CEA", "DAB", "MGD"]
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values))
with arcpy.da.SearchCursor (datasetname,fldname,where) as cur:
&amp;nbsp; for eachrecord in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; regcode = eachrecord.getValue(fldname)
.
.
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;At this point I get the error message "AttributeError: 'tuple' object has no attribute 'getValue'"&lt;BR /&gt;Any suggestions? Cheers, Damian&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Damian,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You are close. The values are retrieved from the row by using an index. If you only provide 1 field as fields in the da.SearchCursor, your value will be at index 0, hence the line: regcode = eachrecord[0]. Just look at the code below where I have introduced more fields to clarify:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;datasetname = lyr.dataSource
fldname = "FIRECODE"
lst_values = ["ARC", "CEA", "DAB", "MGD"]
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values))

# define a tuple (or list) of field names to use in the cursor
flds = (fldname, 'someOtherFieldname', 'andYetAnotherFieldName')

with arcpy.da.SearchCursor(datasetname, flds, where) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for eachrecord in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; regcode = eachrecord[0] # firecode
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; someOtherValue = eachrecord[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; andYetAnotherValue = eachrecord[2]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since you are only using 1 field in a search cursor, I assume you are trying to determine some statistic. If that is the case there maybe faster ways to do that (list comprehensions of using a dictionary). Just let me know what you want to do in the loop and I can give you some pointers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Another powerful feature of the da cursors are the build-in fields like "SHAPE@" for the geometry, "OID@" for ObjectID, etc. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:33:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380812#M30024</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T17:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: searchcursor on layer using text field and query</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380813#M30025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi, I'm trying to use a SearchCursor to loop through specific records in a layer in an ArcMap project.&amp;nbsp; I would like loop through specific records based on a text field in the layer but I'm having trouble figuring out the syntax. Can anyone help please? Code below.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Set the variables
mxd = arcpy.mapping.MapDocument(r"\\working\BioFire.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd,"Fire",df)[0]

# Loop through specified records in layer

### I CAN'T FIGURE OUT THE CODE FOR THIS NEXT BIT
cur = arcpy.SearchCursor(lyr, '"FIRECODE" in ("ARC", "CEA", "DAB", "MGD")')&amp;nbsp; 

### IF I USE THE LINE BELOW INSTEAD IT WORKS FINE BUT LOOPS THROUGH ALL RECORDS WHICH I DON'T WANT
#cur = arcpy.SearchCursor(lyr) 
.
.
.
.

&lt;/PRE&gt;&lt;BR /&gt;Cheers, Damian&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The expression has the wrong quotes for the values.&amp;nbsp; They have to be in single quotes.&amp;nbsp; Change it to this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cur = arcpy.SearchCursor(lyr, """FIRECODE"" in ('ARC', 'CEA', 'DAB', 'MGD')")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:33:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-on-layer-using-text-field-and-query/m-p/380813#M30025</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T17:33:49Z</dc:date>
    </item>
  </channel>
</rss>

