Question on ´GIS Tutorial for Python Scripting´ working with cursorsl

2805
2
Jump to solution
02-24-2015 01:35 PM
Ericvan_Rees
New Contributor

Hello,

 

I´m trying to work my way through the ´GIS Tutorial for Python Scripting´ by David Allen. Working with cursors (Tutorial 2.3) is giving me a hard time as there seem to be a number of ways to approach this subject, but I can´t make any sense of them. What confuses me most is the numbering in the syntax. Can anybody helmp me with Exercise 2.3? The problem seems to be a non-defined cursor name, but I´m lost here. This is the code I´ve come up with so far:

 

import arcpy

from arcpy import env

env.workspace = r"C:\EsriPress\GISTPython\Data\City of Oleander.gdb"

env.overwriteOutput = True

updateFC = "StreetLights"

with arcpy.da.UpdateCursor("StreetLights",["TYPE", "Buffer"]) as cursor:

    for row in cursor:

        TYPE = row[0]

        Buffer = row[1]

        if TYPE == "MV":

            Buffer = 125

        else:

            Buffer = 200

        cursor.updateRow(row)

        print "All done updating"

        del cursor

        del row

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

A few errors in the code.

  • row[1] is the buffer value you try to assign, so you should write to it, without it, it doesn't make sense to update the row, since nothing changes
  • delete the cursor and row outside the with statement, otherwise you are "cutting the branch you're sitting on"

import arcpy
from arcpy import env
env.workspace = r"C:\EsriPress\GISTPython\Data\City of Oleander.gdb"
env.overwriteOutput = True
updateFC = "StreetLights"
with arcpy.da.UpdateCursor(updateFC, ["TYPE", "Buffer"]) as cursor:
    for row in cursor:
        TYPE = row[0]
        if TYPE == "MV":
            row[1] = 125
        else:
            row[1] = 200
        cursor.updateRow(row)
del cursor, row

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

A few errors in the code.

  • row[1] is the buffer value you try to assign, so you should write to it, without it, it doesn't make sense to update the row, since nothing changes
  • delete the cursor and row outside the with statement, otherwise you are "cutting the branch you're sitting on"

import arcpy
from arcpy import env
env.workspace = r"C:\EsriPress\GISTPython\Data\City of Oleander.gdb"
env.overwriteOutput = True
updateFC = "StreetLights"
with arcpy.da.UpdateCursor(updateFC, ["TYPE", "Buffer"]) as cursor:
    for row in cursor:
        TYPE = row[0]
        if TYPE == "MV":
            row[1] = 125
        else:
            row[1] = 200
        cursor.updateRow(row)
del cursor, row
Ericvan_Rees
New Contributor

Hello Xander,

thanks for your clear explanation, this is very helpful.

Eric

0 Kudos