0

ArcGIS Pro 3.0.0 - using ArcPy

First, I'm very new to coding with Python, so there may be a few mistakes in my code that I am not catching. I'm trying to select each row in a table (line feature class) and save each as it's own feature class using the arcpy.management.SelectLayerByAttribute() tool into a feature database ("Test"). I have a "Long" field titled "Unique_ID" that has numbered the rows from 1 to 291, and I have been trying to use that field to identify and name each row.

**I tried using the Split By Attributes tool, but there is an issue with the format of the output, and I am not able to use the output (not recognized as a feature class) in the next steps. I looked into questions about that tool, but have been unable to resolve it, so I am going this route.

The following is the code I have been working with:

import arcpy
arcpy.env.workspace = "C:/ArcPro_Data/Simp_int_test/Simp_int_test.gdb"
featureclass = "C:/ArcPro_Data/Simp_int_test/Simp_int_test.gdb/Simp_BL_out"
field_bl = [f.name for f in arcpy.ListFields(featureclass, "Un*")]
x=1
with arcpy.da.SearchCursor(featureclass, field_bl) as cursor:
    for row in cursor:
        new_val = x
        express = "Unique_ID = new_val"
        layer_out = "C:/ArcPro_Data/Simp_int_test/Simp_int_test.gdb/Test/splitline_" + str(new_val)
        arcpy.management.SelectLayerByAttribute(featureclass, "NEW_SELECTION", express)
        arcpy.management.CopyFeatures(featureclass, layer_out)
        arcpy.management.SelectLayerByAttribute(featureclass, "CLEAR_SELECTION")
        x=x+1

The main issue I run into is that the "Unique_ID" field is not found by the program and is returning this error: "ERROR 160144: An expected Field was not found or could not be retrieved properly" for line 11, the Select By Layer Attribute tool.

I noticed that arcpy.ListFields() was returning locations of the fields like this: [<Field object at 0x1b855cadac0[0x1b855ca9f30]>], instead of the actual field name. So, I included .name to list the exact field I want. I am still running into the same error message and I'm not sure how to identify the correct field in the code and use it in the expression in Select Layer By Attribute.

*I also tried new_val = row.getValue(field_bl) for line 8, and it returns the error: "AttributeError: 'tuple' object has no attribute 'getValue'".


Edit: I looked through questions as Vince suggested and the SQL reference guide that ESRI provides, and I tried to update my query to be in the correct format. However, I am still running into the issue that the field is not found or able to be retrieved. So, what can I do to make my query correct and so it does replace row in the expression?

I incorporated row into my expression, but I have a feeling it's still incorrect.

Updated code:

featureclass = "Simp_BL_out"
x = 1
with arcpy.da.SearchCursor(featureclass, "Unique_ID") as cursor:
    for row in cursor:
        express = """"Unique_ID" = row"""
        layer_out = "C:/ArcPro_Data/Simp_int_test/Simp_int_test.gdb/Test/splitline_" + str(x)
        arcpy.management.SelectLayerByAttribute(featureclass, "NEW_SELECTION", express)
        arcpy.management.CopyFeatures(featureclass, layer_out)
        arcpy.management.SelectLayerByAttribute(featureclass, "CLEAR_SELECTION")
        x=x+1
2
  • 1
    Your code doesn't use the row contents, and you aren't doing replacement in the expression construction. DA SearchCursor doesn't use the kludgey "old cursor" syntax (besides, the getValue is not valid with a list parameter). There's lots of Questions here that show how to format data into expressions -- the documentation has examples as well.
    – Vince
    Commented Nov 1, 2022 at 18:35
  • Look at the documentation, Example 1. This shows you how to use the list captured by row (you specified the order with field_bl; offsets start at zero). There are probably hundreds of posts here that show how to format a query expression with variable parameters. The awful ones will use string math. The good ones will exploit str.format(), like this Q&A
    – Vince
    Commented Nov 2, 2022 at 2:17

1 Answer 1

1

The first thing is to check if Unique_ID is actually the field name and not some alias that was set.

print([field.name for field in arcpy.ListFields(<your feature class>)])

Honestly though, if you are just iterating every row why not just use OBJECTID that comes with every feature class?

Next, your expression doesn't really make sense. row is a tuple. It represents your record with all fields you passed to the SearchCursor. e.g if you had a table of fruits and quantities, your tuple would look like ('apple', 1). If you only pass in one field, it's still a tuple, ('apple'). So Field = tuple won't work for your expression in this case. Access the row data with row[index].

As for the field not found error, see this other post of field delimiters.

Based on your current approach, you are probably looking for something like this:

counter = 1
with arcpy.da.SearchCursor(fc, 'OBJECTID') as search_curs:
    for row in search_curs:
        obj_id = row[0]

        selection_layer = arcpy.management.SelectLayerByAttribute(
            fc, 
            'NEW_SELECTION', 
            "OBJECTID = " + str(obj_id)
        )

        arcpy.CopyFeatures_management(selection_layer, fc + '_' + str(counter))
        
        counter += 1
1
  • Thank you @siushi ! This solved my problem. Thank you for the explanation as well. I still had to use "Unique_ID" as the field because the the ID field was "OID" and the Select tool wasn't registering it for some reason. As I said, coding in python and arcpy is very new to me so I appreciate you taking the time to talk through the things that I was doing wrong.
    – Cass_789
    Commented Nov 2, 2022 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.