Update Cursor Python Programming Issue

1209
10
10-28-2017 02:35 PM
CourtneyWade
New Contributor II

Hi there. I'm a frustrated student taking a programming class where I am way over my head. I am taking this class with my first GIS class (bad idea) so my understanding is very limited. That being said, if you could give clear and simple explanations, I would greatly appreciate it. Thanks!

This is the assignment question:

"For this assignment you will perform a batch update on the trees fc. While the arborists performed their tree inventory of the park, they mistakenly made some data collection errors. Each “Ash” tree that had a DBH greater than 25 should actually be an “Oak” species. Your task is to develop an update cursor/python code that performs this data fix."

This is a screenshot of my attribute table. 

This one of countless attempts:

Tags (1)
0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus

Your first line fails in my IDE using python 3.5

a = "C:\Temp\Unit_8_Assignment\Lake_Short_Park.gdb"

  File "<ipython-input-7-23a9732c065c>", line 1
    a = "C:\Temp\Unit_8_Assignment\Lake_Short_Park.gdb"
       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 7-8: truncated \UXXXXXXXX escape


a = r"C:\Temp\Unit_8_Assignment\Lake_Short_Park.gdb"  # ---- using raw formating

'C:\\Temp\\Unit_8_Assignment\\Lake_Short_Park.gdb'

I didn't get beyond there since I don't have the data to test.

It looks offhand that your row[0] and cursor.update.... row have 5 spaces and not 4... check 

XanderBakker
Esri Esteemed Contributor

Try this:

fc = r'C:\Temp\Unit8_Assignment\Lake_Shore_Park.gdb\trees'
fields = ("DBH", "Common")
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if row[0] > 25 and row[1] == "Ash":
            row[1] = "Oak"
            cursor.updateRow(row)

Your line:

if (row [0] == >25):

is not valid for 2 reasons, and perhaps 3 reasons:

  • the additional space after row
  • the == > which should actually be just >
  • if another species (no Ash) would be larger than 25 it would be changed to Oak and that might not be correct
DanPatterson_Retired
MVP Emeritus

your eyes are better than mine another reason I hate screen grabs of code problems

0 Kudos
CourtneyWade
New Contributor II

Thank you so much! Darn Spacing! I was thinking too that if another species was larger than 25 it would mess it up I just didn't know what to do.  I didn't know I could use the 'and' . This class is online and very difficult to ask for help when I'm working on it. I thank you for taking the time to show and explain it to me. Have a great night. 

XanderBakker
Esri Esteemed Contributor

Hi Courtney Wade , I'm glad we could help.

There is no problem to post other problems you might run in to here in GeoNet. For the next time, please use the instructions found here to copy the code into the thread: Posting code with Syntax Highlighting on GeoNet . Not only since Dan asked, but it normally helps a lot not having to type the code and being able to simply copy and paste it to see what might be wrong.

Also, it would be good if you mark the post that answered the question as the correct answer by clicking on the "Mark Correct" button:

Any post that you consider helpful can be marked as such. Just use the helpful link below the post.

RebeccaLeach
New Contributor III

Xander,

I was wondering if you could explain the assignment of row[0] and row[1].  How do I know that row[0] should go with "DBH" in the table?  Is it based on the fact that "DBH" was listed first in your fields assignment within the python code?  And is it always the case that the first row will always be assigned [0]?

0 Kudos
DanPatterson_Retired
MVP Emeritus

The list of fields that you decide on using determines the slice ie row[0] means for each row using the first column... row[1] means for each row using the 2nd column.  Now since you only provided two columns, and python is zero-based, the first column and second column are ('DBH', 'Common') as a tuple (round brackets) or as a list ['DBH', 'Common'] (square brackets).  Remember zero-based

RebeccaLeach
New Contributor III

Thanks Dan!  I appreciate the information you provided.  It makes sense to me now.

0 Kudos
CourtneyWade
New Contributor II

Darn spacing trips me up all the time! Thank you Dan for taking the time to help me. Have a great night!

0 Kudos