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)
Solved! Go to Solution.
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
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
Is there a way to write this in a 'for' or 'while' loop using either update cursor or deleteRow()?
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
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.
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