2

Every time I execute this, I get a runtime error pointing to my "for row in cursor," stating that A column was specified that does not exist. Any ideas how to fix this?

import arcpy
import os

gdb = r'C:\Users\Stephanie\Documents\SPRING20\PGIS\project1\usa_cities.gdb'
feature_class = "largest_cities"

fc = os.path.join(gdb, feature_class)
print(fc)

fields = ['NAME', 'POP2017']

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        if row[2] != None:
            print("City: " + str(row[2]) + " Population: " + str(row[9]))

enter image description here

5

I think you are trying to access fields that you don't specify, in this case you can access only row[0 for NAME field, and row[1] for POP2017 field. You are using index greatest that 1, that is a mistake. If you want to retrieve all fields use * instead,

with arcpy.da.SearchCursor(fc, '*') as cursor:

BTW, , for performance and order it is recommended to specify the fields.

|improve this answer|||||
  • 1
    I believe what you're trying to say is that indexing of fields rows from an arcpy.da.SearchCursor is 0 based, which is absolutely correct, also if you've specified the fields with a list then only the specified fields can be accessed so trying to grab str(row[9]) isn't going to work if there have only been two fields requested. – Michael Stimson Feb 23 at 23:26
  • @MichaelStimson thanks, that's what I was trying to point. – cabesuon Feb 24 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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