AnsweredAssumed Answered

Update & Delete Cursor Functions

Question asked by Aleah2vw on Jan 31, 2017
Latest reply on Feb 1, 2017 by Aleah2vw

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)

Outcomes