Wednesday 29 November 2017

arcgis desktop - Using UpdateCursor to update a field based on other field values


I've been trying to create a python script that will update certain fields in the arcgis attribute table. I have attempted to do this by using the update cursor to update a field based on another field value found in the same row.


Here is the code I used:


import arcpy

fc = "C:\Users\maureen\Documents\ArcGIS\EDRN\EDRN_LINK.shp"
fields = ["FID", "RIVERNAME"]


with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0] = 4401:
row[1] = "Aire"
cursor.updateRow(row)

Unfortunately, when I run this in ArcMap it returns the following syntax error:


Parsing error SyntaxError: invalid syntax (line 9) 

Does anyone have any idea where I'm going wrong? I've researched this script a fair bit and can't see any obvious errors.




Answer



For comparison of 'equals' you need to use a double equals sign '=='


if  row[0] == 4401:
row[1] = "Aire"
cursor.updateRow(row)

A single equals sign is the assignment operator in python.


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...