<?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: If else statement in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762073#M58823</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I was hoping ESRI or the forums contained attitional information.&amp;nbsp; I also have a book that covers Python and the OBJECTID IN operator is not mentioned.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The IN operatot is not a python expression.&amp;nbsp; It is an SQL expression and it is fine to use with all of the databases and the only efficient way to work with lists of values in SQL.&amp;nbsp; It is also fine to use with Python lists, since Python code that uses the IN expression will be found all over the Python forum by every advanced user of Python here.&amp;nbsp; So ignore that recommendation for both SQL and Python.&amp;nbsp; So use the IN operator as shown in my example or Jakes example (as corrected by me for the arcLength field test) and you will get the idea soon enough about what it does.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 30 Aug 2013 19:21:57 GMT</pubDate>
    <dc:creator>RichardFairhurst</dc:creator>
    <dc:date>2013-08-30T19:21:57Z</dc:date>
    <item>
      <title>If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762028#M58778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am modifying a script suggested in a prior thread.&amp;nbsp; &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/90242-Polygon-Centroid?highlight=jsmith0705" rel="nofollow" target="_blank"&gt;http://forums.arcgis.com/threads/90242-Polygon-Centroid?highlight=jsmith0705&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to be able to select a line, determine if the ARCLENGTH field has a value greater than zero, return that value if greater than zero, if not return the value from the Shape_Length field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script runs with no errors or results.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0]&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd):&amp;nbsp; tlyr = lyr&amp;nbsp; dsc = arcpy.Describe(tlyr)&amp;nbsp; sel_set = dsc.FIDSet&amp;nbsp; if dsc.shapeType == "Line": &amp;nbsp; if len(sel_set) &amp;gt; 0: #If ARCLENGTH value &amp;gt; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(ARCLENGTH)) #Return ARCLENGTH value &amp;nbsp; else: &amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(Shape_Length)) #Return Shape_Length value&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 11:39:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762028#M58778</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2013-08-28T11:39:13Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762029#M58779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try adding some print statements to understand what is actually occurring with your script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print("The describe statement is returning " + dsc) - You mau need to convert dsc to a string if it is not by default a string&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If that returns an expected item then I would continue to add print statements at each critical step to ensure you are getting the expected results.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please provide results of your print statements so I can further assist you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 12:15:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762029#M58779</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-08-28T12:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762030#M58780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi James,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If this is a feature class, try the following below.&amp;nbsp; If this is a shapefile, you will have to update the SearchCursor to use FID instead of OBJECTID.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; tlyr = lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp; dsc = arcpy.Describe(tlyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sel_set = dsc.&lt;STRONG&gt;fidSet&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if dsc.shapeType == "&lt;STRONG&gt;Polyline&lt;/STRONG&gt;":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcLength = row.ARCLENGTH
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shapeLength = row.shape.length
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762030#M58780</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-12T08:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762031#M58781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi James,&lt;BR /&gt;&lt;BR /&gt;If this is a feature class, try the following below.&amp;nbsp; If this is a shapefile, you will have to update the SearchCursor to use FID instead of OBJECTID.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; tlyr = lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp; dsc = arcpy.Describe(tlyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sel_set = dsc.&lt;STRONG&gt;fidSet&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if dsc.shapeType == "&lt;STRONG&gt;Polyline&lt;/STRONG&gt;":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcLength = row.ARCLENGTH
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shapeLength = row.shape.length
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jake and Michael,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using a feature class.&amp;nbsp; The script runs with no errors or results.&amp;nbsp; I am going to try and adjust the SearchCursor and see what happens.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762031#M58781</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2021-12-12T08:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762032#M58782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Why would changing the SearchCursor to arcpy.da.SearchCursor return an SQL error, whereas, arcpy.SearchCursor does not?&amp;nbsp; I thought the only difference was arcpy.da.SearchCursor was more efficient?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 12:58:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762032#M58782</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2013-08-28T12:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762033#M58783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take a look at the help links.&amp;nbsp; The arcpy.da.SearchCursor is formatted differently.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SearchCursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000050000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000050000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.da.SearchCursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you post the exact code you're using in ArcMap?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:02:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762033#M58783</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-08-28T13:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762034#M58784</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

mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames (mxd)[0] 
lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]

for lyr in arcpy.mapping.ListLayers(mxd):
 tlyr = lyr
 dsc = arcpy.Describe(tlyr)
 sel_set = dsc.fidSet
 if dsc.shapeType == "Polyline":
&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp; arcLength = row.ARCLENGTH
&amp;nbsp;&amp;nbsp; shapeLength = row.Shape_Length
&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp; del row
&amp;nbsp; del rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error is in line 32, for row in rows: RuntimeError: An invalid SQL statement was used.&amp;nbsp; I know the select Object ID works, because it changes values depending on the feature selected.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762034#M58784</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2021-12-12T08:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762035#M58785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Check the name of your OBJECTID field (right-click on layer &amp;gt; Properties &amp;gt; Fields tab).&amp;nbsp; It may be OID, or OBJECTID1.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:15:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762035#M58785</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-08-28T13:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762036#M58786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Also, change arcpy.da.SearchCursor, to arcpy.SearchCursor.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:20:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762036#M58786</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-08-28T13:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762037#M58787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Check the name of your OBJECTID field (right-click on layer &amp;gt; Properties &amp;gt; Fields tab).&amp;nbsp; It may be OID, or OBJECTID1.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The field is named OBJECTID.&amp;nbsp; Changing the cursor back to arcpy.SearchCursor eliminates the SQL Error.&amp;nbsp; But, the script runs with no results.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:22:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762037#M58787</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2013-08-28T13:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762038#M58788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you checked to see if some of your earlier statements are getting the desired results such as the Describe?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you get a length of your sel_set to see if that is returning values?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:41:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762038#M58788</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-08-28T13:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762039#M58789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Have you checked to see if some of your earlier statements are getting the desired results such as the Describe?&lt;BR /&gt;&lt;BR /&gt;Can you get a length of your sel_set to see if that is returning values?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Michael,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have used the arcpy.AddMessage.&amp;nbsp; I noticed that when the AddMessage is inserted after the variables it returns the value.&amp;nbsp; But, when I use the AddMessage after the sel_set = dsc.fidSet, the value is returned three times.&amp;nbsp; Which is odd.&amp;nbsp; I need to research why the value is returned three times.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 13:48:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762039#M58789</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2013-08-28T13:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762040#M58790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames (mxd)[0] 
lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]

for lyr in arcpy.mapping.ListLayers(mxd):
 tlyr = lyr
 dsc = arcpy.Describe(tlyr)
 sel_set = dsc.fidSet
 if dsc.shapeType == "Polyline":
&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp; arcLength = row.ARCLENGTH
&amp;nbsp;&amp;nbsp; shapeLength = row.Shape_Length
&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp; del row
&amp;nbsp; del rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;The error is in line 32, for row in rows: RuntimeError: An invalid SQL statement was used.&amp;nbsp; I know the select Object ID works, because it changes values depending on the feature selected.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When you switched back to the original cursor type you got no records because your where clause needs to use the IN operator not the = operator.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The error using the new arcpy.da cursor is due to the cursor declaration syntax being different.&amp;nbsp; With the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy data access (arcpy.da) cursor you have to supply a mandatory field name list&lt;/A&gt;&lt;SPAN&gt; prior to defining an optional where clause.&amp;nbsp; Your code has no field list.&amp;nbsp; At a minimum the field list would have to include OBJECTID, ARCLENGTH and Shape_Length.&amp;nbsp; So this code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;should be changed to this code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set +")")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/SearchCursor/018w00000011000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;OID@&lt;/A&gt;&lt;SPAN&gt; makes sure you get the correct ObjectID field name reference, regardless of the database.&amp;nbsp; Possibly &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;SHAPE@LENGTH should be Shape_Length&lt;/A&gt;&lt;SPAN&gt;, depending on whether you want to access a geodatabase length field or the actual shape geometry length.&amp;nbsp; You also do not have to use field names again after getting the cursor.&amp;nbsp; You can use numbers to represent the fields.&amp;nbsp; So that means the code beyond that line could also change to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")")
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp; arcLength = row.[1]
&amp;nbsp;&amp;nbsp; shapeLength = row.[2]
&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp; del row
&amp;nbsp; del row&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762040#M58790</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-12T08:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762041#M58791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Why are your rows numbered 1 and 2?&amp;nbsp; Should they be 0 and 1.&amp;nbsp; Since, python normally begins the numbering sequence with zero.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;When you switched back to the original cursor type you got no records because your where clause needs to use the IN operator not the = operator.&lt;BR /&gt; &lt;BR /&gt;The error using the new arcpy.da cursor is due to the cursor declaration syntax being different.&amp;nbsp; With the &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy data access (arcpy.da) cursor you have to supply a mandatory field name list&lt;/A&gt; prior to defining an optional where clause.&amp;nbsp; Your code has no field list.&amp;nbsp; At a minimum the field list would have to include OBJECTID, ARCLENGTH and Shape_Length.&amp;nbsp; So this code:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)&lt;BR /&gt;&lt;BR /&gt;should be changed to this code:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set +")")&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/SearchCursor/018w00000011000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;OID@&lt;/A&gt; makes sure you get the correct ObjectID field name reference, regardless of the database.&amp;nbsp; Possibly &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;SHAPE@LENGTH should be Shape_Length&lt;/A&gt;, depending on whether you want to access a geodatabase length field or the actual shape geometry length.&amp;nbsp; You also do not have to use field names again after getting the cursor.&amp;nbsp; You can use numbers to represent the fields.&amp;nbsp; So that means the code beyond that line could also change to:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")")
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;arcLength = row.[1]
&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;shapeLength = row.[2]
&lt;/STRONG&gt;&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp; del row
&amp;nbsp; del row&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762041#M58791</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2021-12-12T08:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762042#M58792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the explanations, information, and suggested script.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to use the Shape_Length field, which is the actual geometry length.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script runs with added arcpy.AddMessages throughout.&amp;nbsp; Yet, the script does not print the ARCLENGTH or Shape_Length value.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;When you switched back to the original cursor type you got no records because your where clause needs to use the IN operator not the = operator.&lt;BR /&gt; &lt;BR /&gt;The error using the new arcpy.da cursor is due to the cursor declaration syntax being different.&amp;nbsp; With the &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy data access (arcpy.da) cursor you have to supply a mandatory field name list&lt;/A&gt; prior to defining an optional where clause.&amp;nbsp; Your code has no field list.&amp;nbsp; At a minimum the field list would have to include OBJECTID, ARCLENGTH and Shape_Length.&amp;nbsp; So this code:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)&lt;BR /&gt;&lt;BR /&gt;should be changed to this code:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set +")")&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/SearchCursor/018w00000011000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;OID@&lt;/A&gt; makes sure you get the correct ObjectID field name reference, regardless of the database.&amp;nbsp; Possibly &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;SHAPE@LENGTH should be Shape_Length&lt;/A&gt;, depending on whether you want to access a geodatabase length field or the actual shape geometry length.&amp;nbsp; You also do not have to use field names again after getting the cursor.&amp;nbsp; You can use numbers to represent the fields.&amp;nbsp; So that means the code beyond that line could also change to:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")")
&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp; arcLength = row.[1]
&amp;nbsp;&amp;nbsp; shapeLength = row.[2]
&amp;nbsp; if arcLength &amp;gt; 0:
&amp;nbsp;&amp;nbsp; print arcLength
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; print shapeLength
&amp;nbsp; del row
&amp;nbsp; del row&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:22:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762042#M58792</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2021-12-12T08:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762043#M58793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How many features do you have in your target feature class?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you print out the fidset, are all of these features included in the semicolon-delimited string?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you post the results of printing the fidset from your dsc object?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 14:53:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762043#M58793</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-08-28T14:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762044#M58794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How many features do you have in your target feature class?&lt;BR /&gt;&lt;BR /&gt;If you print out the fidset, are all of these features included in the semicolon-delimited string?&lt;BR /&gt;&lt;BR /&gt;Can you post the results of printing the fidset from your dsc object?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If the fidset is delimited with semicolons it will have to be changed so that the fid values are delimited by commas for the where clause SQL to work.&amp;nbsp; The SQL IN operator only works with lists of values separated by commas.&amp;nbsp; Nothing in SQL works with semicolon separated lists.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 14:56:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762044#M58794</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2013-08-28T14:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762045#M58795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How many features do you have in your target feature class?&lt;BR /&gt;&lt;BR /&gt;If you print out the fidset, are all of these features included in the semicolon-delimited string?&lt;BR /&gt;&lt;BR /&gt;Can you post the results of printing the fidset from your dsc object?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The feature class has over a million records.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Printing that many records will take quite a while.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The arcpy.AddMessage("Continue") value was returned.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 15:07:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762045#M58795</guid>
      <dc:creator>JamesSmith7</dc:creator>
      <dc:date>2013-08-28T15:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762046#M58796</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;James:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you create a feature class with a small subset of features (say 10) and then print out the fidset?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It appears from Richard's last post that the semi-colon delimited string will need to be converted to a comma separated list.&amp;nbsp; You shouldbe able to find python methods to easily perform that process.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 15:23:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762046#M58796</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2013-08-28T15:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: If else statement</title>
      <link>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762047#M58797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The feature class has over a million records.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Printing that many records will take quite a while.&lt;BR /&gt;&lt;BR /&gt;The arcpy.AddMessage("Continue") value was returned.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If there are a million FID values placed within the IN clause I don't know that there won't be an error.&amp;nbsp; Try it on a much smaller set first.&amp;nbsp; I suspect the cursor won't accept a string that big that it could pass to the query parser and performance would be horrible.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If it is going to process the entire table don't include a where clause.&amp;nbsp; If it is a selection based on some grouped attribute values include those fields in the field list and write a logical SQL expression that gets the selection.&amp;nbsp; If you are doing it based on a spatial query and get a huge list, perhaps try feeding portions of the OID list in a for loop to the query expression if an error gets thrown or performance drops too drastically.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 15:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-else-statement/m-p/762047#M58797</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2013-08-28T15:48:48Z</dc:date>
    </item>
  </channel>
</rss>

