Monday, 8 July 2019

Change specific rows in attribute table using Arcpy in ArcGIS 10.3


I need to change values in field "name1" (string field) in specific rows: 70-72 into "no land use" value. I search Arcpy code to do it.


i red How to select specific row in attribute table using ArcPy (without iterating through whole table)? but didn't understand what shoud i do.


I using this code:


import arcpy

lyrName = r"G:\PROJECTS\herzel138\gis\layers\New File Geodatabase.gdb\digum2"
with arcpy.da.UpdateCursor(lyrName, ["name1"]) as cursor:

for row in cursor:
if row == 70 and row ==71 and row ==72:
row[0] = 'no land use'
cursor.updateRow(row)
print "Processing complete"
else:
print 'nothing founded'

enter image description here


but i get no result:



enter image description here



Answer



You must check the value of the OBJECTID field, not the row object:


import arcpy

lyrName = r"G:\PROJECTS\herzel138\gis\layers\New File Geodatabase.gdb\digum2"
with arcpy.da.UpdateCursor(lyrName, ["name1", "OID@"]) as cursor:
for row in cursor:
if row[1] in (70, 71, 72):
row[0] = 'no land use'

cursor.updateRow(row)
print """Updated row {}""".format(row[1])

The OID@ token refers to the OBJECTID field, no matter how it's called (this may vary depending on the dataset format - file gdb, shapefile, enterprise gdb...). See the arcpy.da.UpdateCursor help page for reference.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...