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?
Why not put this into a script, instead of typing it out each time.
Just open IDLE or whatever you are using, open a new file, put the code there, and save.
You can run it within the interpreter.
Then post the entire code here using the python syntax highlighter.
Perhaps a piece of data as well.
Does that workspace exist? Can the script see it? Its has spaces in the path which is generally a bad thing, but I don't think that is the issue here.
Do you have write permissions in that workspace?
And, what is that workspace anyway? A folder? A fgdb (doesn't look like it)?
If this is a coverage (still), you will not be able to overwrite a system variable like that. And, not sure if Update cursors would work anyway.
if row[1] = "Obodowo": # row[1] is Naz ???
row[2] = 170 # since row[2] is Area
but if area is a text field then use "170"
You might want to write out what for the data currently are in and what you want it to look like
I would like to change the value of the attribute "AREA" if the value of the attribute "NAZ" is "Obodowo"
I have also tried to use such a line of code previously, but it doesn't help.There is still no result and no error message.Have you got maybe other ideas?
String variables accessed within a cursor like this don't need extra quotes:
if row[0] == "Obodowo"
should do it.
Hi Malgorzata,
When you run this, are you getting an error? Or is it not updating any of the rows?
It looks like there may be an issue with your if statement. What are you wanting to see in column [0]? The single quotes are throwing me off. Can you explain that more?
Also, you may want to consider adding your code to the syntax editor by going to "Use advanced editor", click on the double arrows, choose Syntax Highlighting, and choose Python.
How to update the value of a string type attribute?After running the following script, there is no answerCould you please help me?
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.
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
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.
It helps Thank you very much Luke.
Your except clause is indented too far. It should be the same indentation as the try clause.
The script still gives the syntax error message
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:
But your syntax problem is
if row[0] = 4:
if row[0] == 4:
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.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.