Hello,
I am trying to perform some editing on features that have the same attribute value as features of a different feature class. So I tried this:
field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)
for row0 in cursor0:
for row1 in cursor1:
if row0 == row1:
do do editing
else:
skip editing
I works, but only for the first round. Then it stops and does not proceed to the second feature in the feature class. Not sure what is wrong with this script.
I tested the same syntax on a different example:
q = ['a1', 'a2', 'a3']
w = ['a3', 'a1', 'b2']
for x in q:
for y in w:
if x == y:
print x, " YES ", y
else:
print x, " NO ", y
and it works fine.
To my novice eye there is no difference in both scripts except the use arcpy.da.SearchCursor(). Can this be a problem? if not, what else?
Thanks for help.
Solved! Go to Solution.
Have you tried resetting the cursor yet?
field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)
for row0 in cursor0:
for row1 in cursor1:
if row0 == row1:
do do editing
else:
skip editing
cursor1.reset()
Don't have an answer for you Tomasz, but I do have a question; which of the cursors stop? Cursor0 or Cursor1? Just yesterday I was faced with a similar problem where I want to edit one table based on it's interaction with another feature class.
Let's call in the guys that have helped me in the not so distant past with cursors:
curtvprice, dkwiens, Dan_Patterson, jskinner-esristaff
and see what they might say....
sounds like nested cursor issues.
Don't you cursor types use 'while' loops? or do you have to call a next()? I am sure the others have examples, but here is the docs If it data were not in the millions of records, I would just read both into arrays/lists, do the comparisons and keep an index of what to change for processing after, rather than on the go
Darren Wiens provided this link yesterday: /blogs/richard_fairhurst/2014/11/08/turbo-charging-data-manipulation-with-python-cursors-and-diction...
I'm about to get into it....
Overall, the approach is not very efficient, but I only have time now to speak to the specific error you are seeing. You need to reset cursor1 before going to the next row of cursor0. I don't know what is entailed in "do do editing," but you won't be able to do any updates to either data set using search cursors.
Have you tried resetting the cursor yet?
field0 = 'SKMS_tor'
field1 = 'SKMS'
cursor0 = arcpy.da.SearchCursor(fc0, field0)
cursor1 = arcpy.da.SearchCursor(fc1, field1)
for row0 in cursor0:
for row1 in cursor1:
if row0 == row1:
do do editing
else:
skip editing
cursor1.reset()
This is what I needed. Thanks Joshua!
Please mark a response as the answer to close the question out. Also, if you haven't already, give a helpful to other responses that were, well, helpful.
I would also suggest Turbo Charging Data Manipulation with Python Cursors and Dictionaries (see Example 1).
Using a search cursor, you would load a dictionary with values contained in the SKMS_for field along with the objectID or globalID for that dataset.
Then using an update cursor, if the field value is in the dictionary, you can do your editing or mark the row for later action.
Thank you Randy,
Usefull stuff and I need to get into this. Lots of learning for me as a complete novice!