I would like to change the value of one field in the table base on the value from another table
This syntax does not work.
Parsing error SyntaxError: invalid syntax (line 6)
fc = 'Dudley_vehicles'
fields = ['Location', 'Location_E']
with arcpy.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0]=1:
row[1]=392713
cursor.updateRow(row)
CODE
fc = 'Dudley_vehicles'
fields = ['Location', 'Location_E']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0] == 1:
row[1] = 392713
cursor.updateRow(row)
IS GIVING ME:
Parsing error IndentationError: unindent does not match any outer indentation level (line 8)
2 Answers 2
There are a couple of issues with your code.
Use the arcpy.da.Updatecursor
You need to use == operator
The update row part of the code is not indented properly. See below for corrections:
fc = 'Dudley_vehicles' fields = ['Location', 'Location_E'] with arcpy.da.UpdateCursor(fc, fields) as cursor: for row in cursor: if row[0] == 1: row[1] = 392713 cursor.updateRow(row)
-
When I put if row[0]==1 I got Runtime error Traceback (most recent call last): File "<string>", line 8, in <module> StopIteration: iteration not starteduser103910– user1039102017年08月17日 08:29:49 +00:00Commented Aug 17, 2017 at 8:29
-
whiteout knowing their workflow, but I think the
cursor.updateRow(row)
has to be intended one more? (to be inside the if statement)LaughU– LaughU2017年08月17日 08:44:21 +00:00Commented Aug 17, 2017 at 8:44 -
no, that is not correct. As we are creating a if, else statement for the rows, the actual updaterow should sit at that indentation level.MacroZED– MacroZED2017年08月17日 08:46:14 +00:00Commented Aug 17, 2017 at 8:46
-
okay, my bad, I was thinking, that you would update every row, even if you don't change anything, that's why I thought it would be better to place the update row cursor inside the if statementLaughU– LaughU2017年08月17日 08:50:55 +00:00Commented Aug 17, 2017 at 8:50
-
you can create huge if,else statement, then once you're ready to update those particular rows, the cursor.updaterow(row) will make all those changes in one pass. if you had it at next indentation level, you would be updating the table with each line. this is inefficient and defeats the purpose of an updatecursor.MacroZED– MacroZED2017年08月17日 08:56:22 +00:00Commented Aug 17, 2017 at 8:56
Try replacing:
with arcpy.UpdateCursor(fc, fields) as cursor:
with:
with arcpy.da.UpdateCursor(fc, fields) as cursor:
and:
if row[0]=1
with:
if row[0]==1
and take care with your indentation on your last line - the c
is indented one space too far.
-
using arcpy.da.UpdateCursor.... is not working eitheruser103910– user1039102017年08月17日 08:32:14 +00:00Commented Aug 17, 2017 at 8:32
if row[0]==1:
this should do the trick for the syntax error