2

I am trying to use a search cursor to print three columns of data (out of the existing 10). I run a dissolve early in the code and use raw_input so I can specify which fields to use for the dissolve parameter. My endgame is so I can use this on various datasets. Anyway, when I get to the search cursor, it kicks back and error stating "not a spatial reference."

Here is my code after the dissolve. I use the many print field statements to make sure it is printing the text correctly. Ultimately, what I am trying to do is have it automatically use the fields I use for the dissolve as the search cursor. I am not sure how I do not have a spatial reference when codes_fc is the dissolved_lu layer. Dissolve_items contains the field names in the dissolve. They are typed in as LU12;LABEL12;TYPE12 - which explains the replace the in the first line.

textheaders = str(dissolve_items.replace(";",","))
print textheaders

codes_fc = dissolve_lu
fields = textheaders.split(',')
print fields
print fields[0]
print fields[1]
print fields[1]
print fields[2]

#Search fields for using Search Cursor
with arcpy.da.SearchCursor(codes_fc, fields[0], fields[1], fields[2]) as     cursor:
#log("LU12" + "," + "Label_12")
    for rows in cursor:
        print rows
        codes = str(fields[0])
        desc = str(fields[1])
        type1 = str(fields[2])
        print codes, desc, type1
        code_entry = str(codes, desc, type1)
        if code_entry not in lu_code_list:
            lu_code_list.append(code_entry)
print lu_code_list
3

This line has the wrong syntax:

with arcpy.da.SearchCursor(codes_fc, fields[0], fields[1], fields[2]) as     cursor:

You are separating your field names by commas and therefore actually stating they are parameters to the function SearchCursor(). If you read the help file is states that the fields you want to return in a search cursor is supplied as a LIST of field names. So it is not surprising that it is returning an error about spatial reference as a field name is not a spatial reference parameter!

You then do not return the correct values from the row. The code you want is this:

with arcpy.da.SearchCursor(codes_fc, [fields[0], fields[1], fields[2]]) as cursor:
    for row in cursor:
        print row
        codes = str(row[0])
        desc = str(row[1])
        type1 = str(row[2])
        # blah blah blah
  • Ah! I see now. I was trying different permutations of where to split the text by the ",". I mustn't have put them back. It was reading it as if I had more than I input feature class to search. I know better for next time. Thanks! – Smithw1 Jun 14 '17 at 14:10
  • 1
    Could also just use with arcpy.da.SearchCursor(codes_fc, fields) as cursor: as it's already a list of those fields – Midavalo Jun 14 '17 at 14:22

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.