2

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)

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 17, 2017 at 7:59
2
  • 2
    try if row[0]==1: this should do the trick for the syntax error Commented Aug 17, 2017 at 8:20
  • as mentioned, ive updated my answer with the correct indentation. Commented Aug 17, 2017 at 9:30

2 Answers 2

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)
    
answered Aug 17, 2017 at 8:37
5
  • 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 Commented 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) Commented 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. Commented 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 statement Commented 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. Commented Aug 17, 2017 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.

answered Aug 17, 2017 at 8:08
1
  • using arcpy.da.UpdateCursor.... is not working either Commented Aug 17, 2017 at 8:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.