1

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
    try if row[0]==1: this should do the trick for the syntax error – LaughU Aug 17 '17 at 8:20
  • as mentioned, ive updated my answer with the correct indentation. – MacroZED Aug 17 '17 at 9:30
3

There are a couple of issues with your code.

  1. Use the arcpy.da.Updatecursor

  2. You need to use == operator

  3. 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 started – user103910 Aug 17 '17 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 Aug 17 '17 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 Aug 17 '17 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 statement – LaughU Aug 17 '17 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 Aug 17 '17 at 8:56
0

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 either – user103910 Aug 17 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.