Select to view content in your preferred language

Updating rows with an UpdateCursor

5449
18
04-05-2016 01:53 AM
MalgorzataMendela
Occasional Contributor

I would like to update one value of the attribute called "AREA" with the function UpdateCursor, but as I have run the script, I have received a syntax error message.
Could you please help me to solve this problem?error.png     

0 Kudos
18 Replies
NeilAyres
MVP Alum

This looks like an old coverage or something imported into a db.

The fields "AREA" and "PERIMITER" are system variables, so they should not be updated anyway.

If you look right along the attribute table do you have "SHAPE_AREA" or something like that.

That will contain the current area in the units of the feature.

0 Kudos
NeilAyres
MVP Alum

But your syntax problem is

if row[0] = 4:

should be

if row[0] == 4:

MalgorzataMendela
Occasional Contributor

The script still gives the syntax error messageerror.png

0 Kudos
MalgorzataMendela
Occasional Contributor

Unfortunately,  the same problem occurs when I try to change the value of the variable, which isn't a system ones, so this is not the case. Please have a look at the following example:
error.png

0 Kudos
JenniferMcCall4
Frequent Contributor

You seem to be missing the double equal sign in the if statement again.

if row[0] = 114:

should be

if row[0] == 114:

Which would explain why you're getting an error at line 6.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Your except clause is indented too far. It should be the same indentation as the try clause.

MalgorzataMendela
Occasional Contributor

It helps
Thank you very much Luke.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Clicked submit too soon and can't edit my previous comment on the mobile GeoNet site...

The print statement after yo needs to be indented 4 spaces further than the except clause. Move the except back until it's indented the same as the try, then move the print back. Should look something like :

try:

    with arcpy.da.UpdateCursor etc...

        for row etc...

            etc...

except Exception as e:

    print e.message

Better yet...  Remove the try/except completely so you get a proper exception traceback if something goes wrong.

DanPatterson_Retired
MVP Emeritus

Thank you Luke... and to illustrate his point with a simple example

>>> print(something)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'something' is not defined
>>> print(something)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'something' is not defined
>>> try:
...    print(something)
... except Exception as e:
...    print(e.message)
...   
name 'something' is not defined
>>>

Hmmmmm look kind of the same to me.

Learn how to interpret error messages.  You will always get something that you can't decipher easily even with try except blocks (and trackback in different versions)

Your code will be cleaner and indentation errors etc will be less of an issue

0 Kudos