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
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.row
(you specified the order withfield_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 exploitstr.format()
, like this Q&A