Issues with update cursor in Python

4207
7
Jump to solution
02-02-2015 11:44 AM
BrianTweedle
New Contributor

Hi all, I am new to Python and I am currently working on a project where I am attempting to use the update cursor to use a numeric value in a field, and populate a secondary field with text values based off of those numeric values. This code will run successfully, but it will not update the field as desired. My e.message is giving me an __exit__  message. Here is my code:

# Import system modules

import arcpy

from arcpy import env

# Set environment settings

env.workspace = "F:\Geog390\Lab 3\Lab3.gdb"

env.overwriteOutput = True

fc = 'TPSJ'

fields = ('Join_Count', 'Validate')

#'TPSJ' is my feature class, 'Join_Count' is my first field, and 'Validate' is the field I am trying to populate

try:

    with arcpy.UpdateCursor(fc,fields) as cursor:

          for row in cursor:

               if row.getValue[JOIN_COUNT] == 1:

                   row.setValue[VALIDATE] = "Validated"

               else:

                   row.setValue[VALIDATE] = "Not Validated"

    cursor.updateRow(row)

except Exception, e:

    print e.message

any help would be greatly appreciated, thanks!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

Its hard to tell from the pasted code but you may have a code indentation issue on the updateRow() line.

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
for row in cursor:
  # field2 will be equal to field1 multiplied by 3.0
  row.setValue(field2, row.getValue(field1) * 3.0)
  cursor.updateRow(row)

Note in this sample how the cursor.updateRow(row) line is nested within the for row in cursor loop.

View solution in original post

7 Replies
DarrenWiens2
MVP Honored Contributor

If you want to update for each row, place cursor.updateRow(row) at the same indentation level as the if/else.

OwenEarley
Occasional Contributor III

Its hard to tell from the pasted code but you may have a code indentation issue on the updateRow() line.

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
for row in cursor:
  # field2 will be equal to field1 multiplied by 3.0
  row.setValue(field2, row.getValue(field1) * 3.0)
  cursor.updateRow(row)

Note in this sample how the cursor.updateRow(row) line is nested within the for row in cursor loop.

HunterWest
New Contributor III

The aforementioned updateRow indentation error is probably the most likely fix, however, I would mess around with the format of your "field" definition as well.

Currently you have your fields formatted as a tuple rather than a list. I've never used tuples for cursor field lists (so I don't know how well they work or not), but I do know that the "fields" category in cursors can be touchy with regards to formatting.

I would try this instead and see if it works:

fields = 'Join_Count; Validate'

Good luck

0 Kudos
BrianTweedle
New Contributor

I decided to forego the try/except statements and I just used the for loop. My main issue was in fact the indentation of my updateRow() line. I have successfully completed this using this code:

# Import system modules

import arcpy

from arcpy import env

# Set environment settings

env.workspace = "F:\Geog390\Lab 3\Lab3.gdb"

env.overwriteOutput = True

cur = arcpy.UpdateCursor("TP_SJ2")

for row in cur:

    if row.JOIN_COUNT == 1:

        row.VALIDATE = "Validated"

    else:

        row.VALIDATE = "Not Validated"

        cur.updateRow(row)

    del row

del cur

Thanks all for the help!

0 Kudos
OwenEarley
Occasional Contributor III

Just a quick note - your updateRow will not be called for 'Validated' rows unless you drop the indentation back one level:

# Import system modules
import arcpy
from arcpy import env


# Set environment settings
env.workspace = "F:\Geog390\Lab 3\Lab3.gdb"
env.overwriteOutput = True
cur = arcpy.UpdateCursor("TP_SJ2")
for row in cur:
    if row.JOIN_COUNT == 1:
        row.VALIDATE = "Validated"
    else:
        row.VALIDATE = "Not Validated"
        cur.updateRow(row) # < this will not be called for 'Validated' rows
    del row
del cur

Final version should be:

# Import system modules
import arcpy
from arcpy import env


# Set environment settings
env.workspace = "F:\Geog390\Lab 3\Lab3.gdb"
env.overwriteOutput = True
cur = arcpy.UpdateCursor("TP_SJ2")
for row in cur:
    if row.JOIN_COUNT == 1:
        row.VALIDATE = "Validated"
    else:
        row.VALIDATE = "Not Validated"
    cur.updateRow(row)
    del row
del cur
BrianTweedle
New Contributor

Ah yes, thank you as it was only updating what was in my else statement.

0 Kudos
RogerDunnGIS
Occasional Contributor II

This is neat.  I've never seen cursors used that way before.  I always thought that row was a tuple, so I always access it by index.  I've never used the dot notation to access fields before and never knew they had methods like setValue.  What I've been doing is declaring a list of fields I want in the cursor and assigning that to a variable.  Then in the loop I'll write something like row[lstFields.index("MYFIELD")] = someValue.  Thanks for sharing!

0 Kudos