Select to view content in your preferred language

UpdateCursor

466
5
11-01-2023 03:42 AM
stewart
New Contributor II

I am trying to update my attribute field based on a dictionary (join_count_dict) containing {FID:Join_Count}, (eg. {0:1,2:3,3:0,...}
The shapefile has an equal amount of FID as the dictionary, my script works occassionally, any suggestions?

with arcpy.da.UpdateCursor(shapefile, ['FID','Join_Count']) as cursor:

   for row in cursor:
      FID=row[0]
      if FID in join_count_dict:
         row[0] += join_count_dict[fid]
         cursor.updateRow(row)

Any help is much appreciated!

0 Kudos
5 Replies
stewart
New Contributor II

should it rather be 

with arcpy.da.UpdateCursor(shapefile, ['FID','Join_Count']) as cursor:

   for row in cursor:
      FID=row[0]
      if FID in join_count_dict:
         row[1] += join_count_dict[fid]
      else:
         row[1] = -1
      cursor.updateRow(row)
         cursor.updateRow(row)

0 Kudos
BlakeTerhune
MVP Regular Contributor

This should technically work, but you have an extra line calling updateRow() that should be removed. I don't know if the logic is what you want to happen though. Can you clarify exactly what happens when it doesn't work? Is there an error message?

stewart
New Contributor II

There is something else that was ruining it, the extra line is due my poor abilities on posting here. Thanks for the attention though, much appreciated.

0 Kudos
VinceAngelo
Esri Esteemed Contributor

Please put your code in code blocks (click on Post options, choose "Edit post (or reply)", hit the "..." in the top-right of the editor menu (to expand the menu block), copy the code text, then click on "</>" and paste into the popup, then clean up the code inside and outside the code block). This will make your code legible.

- V

StaticK
MVP

Is fid declared anywhere?  I see FID, but python is case sensitive so what is fid in join_count_dict[fid]?