I want to query a feature class on attribute 'state' == 'new', and if so, add that feature class record to a list (I will process this list later).
I have:
cursor = arcpy.da.SearchCursor(points, 'state')
for row in cursor:
if row[0] == 'new':
new_point_list.append(row)
('points' is the feature class, 'state' is the attribute within the feature class) but that just makes a list of tuples, not feature class records!
I ultimately want a list of records in that feature class with a status of 'new', so that I can loop through them and perform other functions.
1 Answer 1
new_point_list.append(row[0])
. But im guessing you want to append objectids (or some other unique identifier) instead, if you want to do more processing later.
If so you can try something like this:
oids = []
with arcpy.da.SearchCursor(points, ['state','OID@']) as cursor:
for row in cursor:
if row[0] == 'new':
oids.append(row[1])
-
1Best done within a
with
or using adel
, no?Vince– Vince2019年01月06日 18:33:51 +00:00Commented Jan 6, 2019 at 18:33 -
If the source is file geodatabase, there won't be a huge time difference with
where_clause="state = 'new'"
but if RDBMS or lots of rows, it could.Vince– Vince2019年01月06日 18:56:34 +00:00Commented Jan 6, 2019 at 18:56 -
But this only gets me a list of ints, not actual records. Is there not a way to just build a table or records, and work with the records directly (instead of re-querying on OID to perform additional steps, for which I have to compare various other attributes and gets complicated)?m.Walker– m.Walker2019年01月06日 19:51:29 +00:00Commented Jan 6, 2019 at 19:51
-
1Use Select tool, or make feature layer tool. No need for cursorBera– Bera2019年01月06日 19:53:08 +00:00Commented Jan 6, 2019 at 19:53
SearchCursor
accepts awhere_clause
parameter, so you don't even need to test therow[0]
value. You'll still have the issue of only saving the field with "new" in it.