0

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" or "y" (I need to use: Update cursor and deleteRow method)

  • For the records that do not equal "y" 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".

Current error: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 26, in File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 19, in selectSchools TypeError: 'Row' object does not support indexing

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)
    row = update.next()
    for row in update:
        row.setValue(where_c)
        updates.updateRow(row)        
    if (row[0].upper() != 'y'):
        row.deleteRow(row)
        print ("NAME")
        row = update.next()

selectSchools(work,sType,outFile)
4
  • 2
    Can you please edit your question and include the error message in full including line numbers.
    – Midavalo
    Feb 1, 2017 at 15:34
  • 1
    I suggest you make a sample dataset of a few records and then trim down your code to just the cursor and see if it works. It looks to me like you're mixing the different cursor types. I recommend using arcpy.da.UpdateCursor() rather than arcpy.UpdateCursor(). See ArcGIS Desktop Help - Update Cursor
    – Midavalo
    Feb 1, 2017 at 15:36
  • 1
    your if (row[0].upper() != 'y'): block should be indented further
    – Midavalo
    Feb 1, 2017 at 15:36
  • if (row[0].upper() != 'y'): will always result in true as y is not uppercase
    – Midavalo
    Feb 1, 2017 at 15:38

1 Answer 1

3

Your code doesn't make sense to me, but this is what I think you are trying to do.

  1. Creates new shapefile based on query
  2. Update Cursor runs on new shapefile
  3. If VERIFIED field value isn't Y or y then print the NAME and then delete the record

This code snippet may help you get going

outFile = "output.shp"
fc = "Schools.shp"

sType = "JUNIOR HIGH"
query = """ "FACILITY" = '{0}' """.format(sType)
arcpy.Select_analysis(fc, outFile, query)

with arcpy.da.UpdateCursor(outFile, ['FACILITY', 'VERIFIED', 'NAME']) as uCursor:
    for row in uCursor:
        if row[1].upper() != 'Y':
            print row[2]
            uCursor.deleteRow()
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.