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!
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)
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?
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.
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
Is fid declared anywhere? I see FID, but python is case sensitive so what is fid in join_count_dict[fid]?