UpdateCursor Problem

3983
8
02-05-2016 09:13 AM
KennethKirkeby
New Contributor III

Disclosure: Portion of my homework

The following script creates a count of wells per county and works:

*********************************************************************************************************************

import arcpy

arcpy.overWriteOutput = True

arcpy.env.workspace = "C:\\Users\\kkirkeby\\Desktop\\GIS-4080\\Lesson5_Data\\Lesson5_Data"

# Variables

wells = "Wells.shp"

counties = "COUNTIES.shp"

Wells_Intersect = "Wells_Intersect.shp"

Wells_Intersect_Layer = "Wells_Intersect_Layer"

input_f = ["Wells.shp", "COUNTIES.shp"]

# Process: Intersect

#arcpy.Intersect_analysis(input_f, Wells_Intersect, "ALL", "", "INPUT")

# Process: Make Feature Layer

#arcpy.MakeFeatureLayer_management(Wells_Intersect, Wells_Intersect_Layer)

# List County Names

countyList = [row[0] for row in arcpy.da.SearchCursor(counties, "COUNTY")]

# Count wells for each county

for cname in countyList:

    whereclause = "{} = '{}'".format("COUNTY", cname)

    wellCnt = 0

    with arcpy.da.SearchCursor(Wells_Intersect_Layer, "COUNTY",whereclause) as cursor:

        for row in cursor:

            wellCnt = wellCnt + 1

        print cname + ", " + str(wellCnt)

*********************************************************************************************************************

I need to update the second to last column in the row with wellCnt. My thought was to modify the script with the following:

forcname in countyList:

    whereclause = "{} = '{}'".format("COUNTY", cname)

    wellCnt = 0

    with arcpy.da.UpdateCursor(Wells_Intersect_Layer, "COUNTY",whereclause) as cursor:

        for row in cursor:

            wellCnt = wellCnt + 1

        row[-2] =  wellCnt

        cursor.updateRow(row)

Error: IndexError: list assignment index out of range

Thanks for any advice or assistance.

Tags (2)
0 Kudos
8 Replies
WesMiller
Regular Contributor III

To use an row index you'll need to supply the fields to your da.UpdateCursor see the help on UpdateCursor—Help | ArcGIS for Desktop

0 Kudos
DanPatterson_Retired
MVP Emeritus

Use raw notation for paths..

r"C:\\Users\\kkirkeby\\Desktop\\GIS-4080\\Lesson5_Data\\Lesson5_Data"

little r in front and next time make sure that you aren't using - or spaces... _ is ok

since it is homework

EDITS

removed the double-backslashes due to a copy error, it should read

r"C:\Users\kkirkeby\Desktop\GIS-4080\Lesson5_Data\Lesson5_Data"

DanPatterson_Retired
MVP Emeritus

from the help

fields = ['ROAD_TYPE', 'BUFFER_DISTANCE']  # Create update cursor for feature class

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

As indicated in the previous post, you need to provide a list of fields to the cursor.  You are trying to access the 2nd last element, however, that is why you are getting an out of range error, because, it doesn't even have a list to draw from

UpdateCursor—Help | ArcGIS for Desktop

RebeccaStrauch__GISP
MVP Emeritus

the help 10.3 -> 10.4 seems to be in place (as I tried to hit your link for my purposes...)

Accessing data using cursors—Help | ArcGIS for Desktop

UpdateCursor—Help | ArcGIS for Desktop   arcpy functions

UpdateCursor—Help | ArcGIS for Desktop    data access module

just an update....fwiw.

0 Kudos
DanPatterson_Retired
MVP Emeritus

They are ... flakifying ... the help today... I can't even get a hit on cursors, let alone any specific type of cursor

0 Kudos
DanPatterson_Retired
MVP Emeritus

yes... copied the wrong testing variant  double backslashes works was testing too much

Filenames and file paths in Python

EDIT

fixed the original

DarrenWiens2
MVP Honored Contributor

As you have it now, you are only passing one field into the cursor - "COUNTY". This means that list of values in row only has one item - the county. row[0] would equal the county for that record. Change "COUNTY" to whatever the field you want to update is called. Then, it will be row[0].

DanPatterson_Retired
MVP Emeritus

Kenneth, at this stage, it would also be worth while looking at Joshua Bixby    blog... he has several on cursors and their use.  This is one, check them all  J

The Iterable Cursor:  Python Built-ins & Itertools