0

I'm trying to update rows using the update cursor, but based on the number of rows in a sequence of three list items. Looking at the list below, it needs to update first four rows to 'B', then the next 9 rows to 'D' and then the next 14 rows to 'A'.

I've gotten as far as the code below, but all I'm getting is an update of the first four to 'B' and then I get 5 calculated to 'D' and then another 5 to 'A'. I think it's going back to the start of the table somehow.

RowCount = 1
lenClusters = [4,9,14] # number of rows to update in that sequence.
rows = arcpy.UpdateCursor(fcPhotos, "", "", "CapturedStatus")

for row in rows:
    if rowCount > 0 and rowCount <= lenClusters[0]: # See if the current rowCount is from 1 to 4
        row.CapturedStatus = "B"
    elif rowCount > lenClusters[0] and rowCount <= lenClusters[1]: # See if the current rowCount is from 5 to 9
        row.CapturedStatus = "D"
    elif rowCount > lenClusters[1] and rowCount <= lenClusters[2]:  # See if the current rowCount is from 10 to 27
        row.CapturedStatus = "A"
    rows.updateRow(row)
    rowCount += 1

I also can't rely on the row ID as it might have missing IDs.

1

From your question, update first four rows to 'B', then starting on the 5th, update 9 rows to 'D' and then finally starting on the 14th row, update 14 rows to 'A', coming to a total of 27 rows:

RowCount = 1
lenClusters = [4,9,14]
rows = arcpy.UpdateCursor(fcPhotos, "", "", "CapturedStatus")

for row in rows:
    if rowCount <= lenClusters[0]:
        row.CapturedStatus = "B" # rows 1 to 4
    elif rowCount > lenClusters[0] and rowCount <= (lenClusters[1] + lenClusters[0]):
        row.CapturedStatus = "D" # rows 5 to 13 (4+9)
    elif rowCount > (lenClusters[1] + lenClusters[0]) and rowCount <= (lenClusters[0] + lenClusters[1] + lenClusters[2]):
        row.CapturedStatus = "A" # rows 14 to 27 (4+9+14 rows) 
    rows.updateRow(row)
    rowCount += 1

That's good code, just your break values were incorrect - or I don't understand your requirements correctly. It's not 27 rows, it's 28 or the last break is only 13 rows.

  • Hi @Michael Miles-Stimson, thanks for the reply. I think I might have worded the question poorly, but the list lenClusters contains the number of rows for each group. It comes from another piece of code I have. I then use that to calculate the rows of a table to either B, D or A in that order, but by the number in the list at that position So B will be the first 4, D will be the following 9 and A will be the last 14. Hope that helps clarify it. I'll change the question above if that helps. – MierMoto May 23 '14 at 4:59
  • It does, you change the question and I'll change the answer. – Michael Stimson May 23 '14 at 5:01
  • Excellent, you're star! Thanks for the solution. I was thinking of doing something along similar lines, but I kept on asking myself why it would be going back to the start, as surely it would just be stepping through the rows. Anyway, your answer works perfectly. Thanks again. – MierMoto May 26 '14 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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