<?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: Search Cursor not obeying selection rules in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642856#M74611</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/895667"&gt;@rsnider43&lt;/a&gt;.,&lt;/P&gt;&lt;P&gt;You need to clean up your code, you have a nested SearchCursor an using "as cursor" and "row" twice. Keep these separate for each SearchCursor call. See below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;## iterate over the fiber_fc
## use the geometry of each line in the Select by Location
with arcpy.da.SearchCursor(fiber_fc, ["comments", "SHAPE@"]) as s_cursor_1:
    ## for each row in the Search Cursor
    for s1_row in s_cursor_1:
        ## select the Splice Points that intersect with the cable
        splice_selections = arcpy.management.SelectLayerByLocation(
            in_layer=splice_fc,
            overlap_type="INTERSECT",
            select_features=s1_row[1], # using the geometry of the fibre
            selection_type="NEW_SELECTION"
        )

        ## iterate over the splice point selection with a second Search Cursor
        with arcpy.da.SearchCursor(splice_selections, ["closure_name"]) as s_cursor_2:
            ## for each splice point
            for s2_row in s_cursor_2:
                ## get the last two characters
                last_2 = s2_row[0].split('_')[3]
                arcpy.AddMessage(last_2)

     ## I would probably make the first SearchCursor an Update Cursor and just
     ## update the Fibre comments here.&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 18 Aug 2025 13:30:04 GMT</pubDate>
    <dc:creator>Clubdebambos</dc:creator>
    <dc:date>2025-08-18T13:30:04Z</dc:date>
    <item>
      <title>Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1641946#M74608</link>
      <description>&lt;P&gt;Hello, I have a script that uses a search cursor to go to a selected feature in a table called SplicePoints_test, that is selected based on it intersecting with some fibercable. After that I use the selected feature and extract the last 2 digits of the string of the selected entry in the SplicePoints_test layer. However when I use the searchcursor to extract the closure_name row, it iterates through all splice points and only appends the last one in the table. I used a break statement to get it to stop iterating, but it only goes to the first one instead, any ideas on how I might get the search cursor to only acknowledge the selected feature?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor("Fibercable_Test", ['comments']) as cursor:
    # Iterate through the rows
    
        for row in cursor:
            arcpy.management.SelectLayerByLocation(
            in_layer="SplicePoints_test",
            overlap_type="INTERSECT",
            select_features="Fibercable_test",
            search_distance=None,
            selection_type="NEW_SELECTION",
            invert_spatial_relationship="NOT_INVERT"
            )
            #iterate through the selected splice point, and split the last digit
            with arcpy.da.SearchCursor("SplicePoints_Select", ['closure_name']) as cursor:
                for row in cursor:
                    last_2 = row[0].split('_')[3]
                    arcpy.AddMessage(last_2)
            
            # add split string to the end of the comments field    
            expr = "'{}{}{}'".format(entry, '.', last_2)
            arcpy.management.CalculateField(
            in_table="Fibercable_test",
            field="comments",
            expression=expr,
            expression_type="PYTHON3",
            code_block="",
            field_type="TEXT",
            enforce_domains="NO_ENFORCE_DOMAINS"
            )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Aug 2025 13:08:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1641946#M74608</guid>
      <dc:creator>rsnider43</dc:creator>
      <dc:date>2025-08-14T13:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642006#M74609</link>
      <description>&lt;P&gt;You’re selecting on a feature class, then cursors read the entire table. Make a feature layer first—SearchCursor will then honor the selection. Also don’t reuse cursor/row names and use next() if you only want the first selected splice point.&lt;/P&gt;&lt;P&gt;Try this pattern:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Make layers so selections stick
arcpy.management.MakeFeatureLayer("Fibercable_Test",  "fc_lyr")
arcpy.management.MakeFeatureLayer("SplicePoints_test","sp_lyr")

with arcpy.da.SearchCursor("fc_lyr", ["OID@", "SHAPE@", "comments"]) as fc_cur:
    for oid, geom, comments in fc_cur:
        # select splice points intersecting the current fiber feature
        arcpy.management.SelectLayerByLocation(
            in_layer="sp_lyr",
            overlap_type="INTERSECT",
            select_features="fc_lyr",  # selection is the current feature because fc_lyr is selected by OID below
            selection_type="NEW_SELECTION"
        )
        # limit fc_lyr to the current feature so location uses just this one
        arcpy.management.SelectLayerByAttribute("fc_lyr", "NEW_SELECTION", f"OBJECTID = {oid}")

        # read ONLY the selected splice points
        last_2 = None
        with arcpy.da.SearchCursor("sp_lyr", ["closure_name"]) as sp_cur:
            row = next(sp_cur, None)  # first selected
            if row and row[0]:
                parts = row[0].split("_")
                if len(parts) &amp;gt; 3:
                    last_2 = parts[3][-2:]  # or parts[3] if exactly two digits

        if last_2:
            expr = f"'{comments}.{last_2}'"
            arcpy.management.CalculateField(
                in_table="fc_lyr", field="comments",
                expression=expr, expression_type="PYTHON3"
            )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Key points:&lt;/P&gt;&lt;P&gt;MakeFeatureLayer for both inputs; run SelectLayerByLocation on layers, not raw FCs.&lt;/P&gt;&lt;P&gt;SearchCursor("sp_lyr", …) will return only selected rows.&lt;/P&gt;&lt;P&gt;Use SelectLayerByAttribute to isolate the current fiber feature by OID before the spatial select.&lt;/P&gt;&lt;P&gt;Use next() to avoid looping and accidentally grabbing the last record.&lt;/P&gt;&lt;P&gt;If multiple splice points can intersect one fiber and you need a specific one, add an order (e.g., nearest) or filter before next()&lt;/P&gt;</description>
      <pubDate>Thu, 14 Aug 2025 15:23:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642006#M74609</guid>
      <dc:creator>VenkataKondepati</dc:creator>
      <dc:date>2025-08-14T15:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642155#M74610</link>
      <description>&lt;P&gt;Might be misunderstanding - but I feel a spatial join then a calculate field would work.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Aug 2025 20:20:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642155#M74610</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2025-08-14T20:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642856#M74611</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/895667"&gt;@rsnider43&lt;/a&gt;.,&lt;/P&gt;&lt;P&gt;You need to clean up your code, you have a nested SearchCursor an using "as cursor" and "row" twice. Keep these separate for each SearchCursor call. See below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;## iterate over the fiber_fc
## use the geometry of each line in the Select by Location
with arcpy.da.SearchCursor(fiber_fc, ["comments", "SHAPE@"]) as s_cursor_1:
    ## for each row in the Search Cursor
    for s1_row in s_cursor_1:
        ## select the Splice Points that intersect with the cable
        splice_selections = arcpy.management.SelectLayerByLocation(
            in_layer=splice_fc,
            overlap_type="INTERSECT",
            select_features=s1_row[1], # using the geometry of the fibre
            selection_type="NEW_SELECTION"
        )

        ## iterate over the splice point selection with a second Search Cursor
        with arcpy.da.SearchCursor(splice_selections, ["closure_name"]) as s_cursor_2:
            ## for each splice point
            for s2_row in s_cursor_2:
                ## get the last two characters
                last_2 = s2_row[0].split('_')[3]
                arcpy.AddMessage(last_2)

     ## I would probably make the first SearchCursor an Update Cursor and just
     ## update the Fibre comments here.&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 18 Aug 2025 13:30:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1642856#M74611</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-08-18T13:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643633#M74628</link>
      <description>&lt;P&gt;You can actually accomplish this whole thing using cursors:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy.da import SearchCursor, UpdateCursor

with UpdateCursor("Fibercable_Test", ['OID@', 'SHAPE@', 'comments']) as cur:
    for oid, cable, comment in cur:
        int_closures: list[str] = [
            closure_name.split('_')[-1]
            for closure_name, in
            SearchCursor(
                "SplicePoints_test", ['closure_name'], 
                spatial_filter=cable)
        ]
        if not int_closures:
            continue
        
        print(f"Found {len(int_closures)} closures for cable {oid}")
        
        # Your original code only stores the last intersecting splice
        # You may want to join them?
        all_closures = f"({','.join(int_closures)})"
        cur.updateRow((cable, f"{comment}.{int_closures[-1]}"))&lt;/LI-CODE&gt;&lt;P&gt;Edit: typo in that script, I forgot to add oid to the updateRow call. It should be the first element of the tuple.&lt;/P&gt;&lt;P&gt;Cursors accept geometry objects as spatial filters! Which means you can skip using layer selections and just keep it compact.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: Made some minor changes after re-reading your script and realizing that you just drop all but the last intersecting splice closure.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 21:15:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643633#M74628</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-08-20T21:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643634#M74629</link>
      <description>&lt;P&gt;Beautiful! I always forget about the (not so) new spatial_filter. Very neat.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 11:55:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643634#M74629</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-08-20T11:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: Search Cursor not obeying selection rules</title>
      <link>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643660#M74630</link>
      <description>&lt;P&gt;In my experience it can sometimes be significantly faster than SelectByLocation or a full iterative filter. Here's a quick test using my cursor module that wraps all that in a nice interface:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HaydenWelch_0-1755693108817.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/138899iB79C07DCC451D083/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HaydenWelch_0-1755693108817.png" alt="HaydenWelch_0-1755693108817.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The top call is getting a count of features that intersect the indexed polygon using the Cursor spatial_filter and the last one is using a SelectByLocation.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Aug 2025 12:32:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-cursor-not-obeying-selection-rules/m-p/1643660#M74630</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-08-20T12:32:48Z</dc:date>
    </item>
  </channel>
</rss>

