Update & Delete Cursor Functions

1643
5
Jump to solution
01-31-2017 06:06 PM
AleahWorthem
New Contributor III

In my program:

-I am selecting only schools of the specified type based on the "FACILITY" field and creating new
feature class of the result.
- Deleting records from the resulting feature class from "Select" where the "Verified" field
does not equal "Y" (I need to use: Update cursor and deleteRow method)
- While deleting records, print the name of each deleted school

My code is not deleting the features that are not equal to "Y". And I am not sure how to print out the values that do not equal "Y".

import arcpy

work = arcpy.env.workspace = "c:\Scripts\Lab 6 Data"
sType = "JUNIOR HIGH"
outFile = "output.shp"
arcpy.env.overwriteOutput = True

def selectSchools(work,sType, outFile):
    fc = "Schools.shp"
    arcpy.MakeFeatureLayer_management(fc, "schoolslyr")
    query = "\"FACILITY\" = '{0}'".format(sType)
    arcpy.Select_analysis(fc, outFile, query)
    arcpy.AddMessage("Select_Analysis layer tool completed!")
    
    where_c = "\"VERIFIED\" = 'y%'"
    update = arcpy.UpdateCursor(outFile, where_c)
    row = update.next()
    for row in update:
         update.setValue(where_c)
         update.updateRow(row)
     print("The uppdate row was sucessful!")
     print ("The following schools have not been verefied: ")

selectSchools(work,sType,outFile))

Updated Code:

import arcpy

work = arcpy.env.workspace = "c:\Scripts\Lab 6 Data"
sType = "JUNIOR HIGH"
outFile = "output.shp"
arcpy.env.overwriteOutput = True

def selectSchools(work,sType, outFile):
fc = "Schools.shp"
arcpy.MakeFeatureLayer_management(fc, "schoolslyr")
query = "\"FACILITY\" = '{0}'".format(sType)
arcpy.Select_analysis(fc, outFile, query)
arcpy.AddMessage("Select_Analysis layer tool completed!")

with arcpy.da.UpdateCursor(outFile, ['VERIFIED', 'NAME']) as uCursor:
for row in uCursor:
if row[0].upper() != 'y%':
print row[1]
if (row[0].upper() != 'y%'):
uCursor.deleteRow()
selectSchools(work,sType,outFile)

1 Solution

Accepted Solutions
AleahWorthem
New Contributor III

Corrected Working Code:

import arcpy

try:
work = arcpy.env.workspace = raw_input("Enter the full workspace path: ")
sType = "JUNIOR HIGH"
outFile = "output.shp"
arcpy.env.overwriteOutput = True

def selectSchools(work,sType, outFile):
fc = "Schools.shp"
arcpy.MakeFeatureLayer_management(fc, "schoolslyr")
query = "\"FACILITY\" = '{0}'".format(sType)
arcpy.Select_analysis(fc, outFile, query)

with arcpy.da.UpdateCursor(outFile, ['VERIFIED', 'NAME']) as uCursor:
for row in uCursor:
if row[0] != 'y':
print ("The following schools have not been verified: "), row[1]
if (row[0] != 'y'):
uCursor.deleteRow()

selectSchools(work,sType,outFile)

except Exception as e:
print "ERROR IN FUNCTION 'selectSchools' " + str("") # Prints Python-related errors
print arcpy.GetMessages() # Prints ArcPy-related errors
arcpy.AddError(e) # Adds errors to ArcGIS custom tool

View solution in original post

5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

perhaps you want

>>> a = 'yes'
>>> (a[0]).upper() == 'Y'
True

This example just checks to see if the first character is y or Y... if you want those that don't ... just use ... != ... in place of equality

AleahWorthem
New Contributor III

Is there a way to write this in a 'for' or 'while' loop using either update cursor or deleteRow()?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

of course... via demonstration

a = ['yea', 'neay', 'Yup', 'Nope', 'yes', 'Yes']
for value in a:
    if (value[0].upper() ==  'Y'):
        print("Y")
    else:
        print("no")

yielding
 Y
no
Y
no
Y
Y
AleahWorthem
New Contributor III

My updated code is now printing out the names of the facilities that do not equal "y" which is good. But when I try to delete those same names from my rows, it deletes all of my records.

0 Kudos
AleahWorthem
New Contributor III

Corrected Working Code:

import arcpy

try:
work = arcpy.env.workspace = raw_input("Enter the full workspace path: ")
sType = "JUNIOR HIGH"
outFile = "output.shp"
arcpy.env.overwriteOutput = True

def selectSchools(work,sType, outFile):
fc = "Schools.shp"
arcpy.MakeFeatureLayer_management(fc, "schoolslyr")
query = "\"FACILITY\" = '{0}'".format(sType)
arcpy.Select_analysis(fc, outFile, query)

with arcpy.da.UpdateCursor(outFile, ['VERIFIED', 'NAME']) as uCursor:
for row in uCursor:
if row[0] != 'y':
print ("The following schools have not been verified: "), row[1]
if (row[0] != 'y'):
uCursor.deleteRow()

selectSchools(work,sType,outFile)

except Exception as e:
print "ERROR IN FUNCTION 'selectSchools' " + str("") # Prints Python-related errors
print arcpy.GetMessages() # Prints ArcPy-related errors
arcpy.AddError(e) # Adds errors to ArcGIS custom tool