Select to view content in your preferred language

"Search Cursor() got an unexpected keyword argument 'sort_fields'???

2527
17
Jump to solution
04-22-2013 06:01 AM
MichelleCouden1
Deactivated User
I have this program to go through layers in an mxd and search for missing values. For Example, it sorts through the layer AADT in the FLAG field and then sorts through the layer AADTAnnoLabel in the TFLAG field to compare for missing values. I am getting a sort fields error. Could someone please explain that to me. What am I doing wrong!! I am not finding help online when I search for it. Thanks!!

import arcpy  flayer = "AADT" alayer = "AADTAnnoLabel"  FRows = arcpy.SearchCursor(flayer,"", sort_fields="FLAG A") ARows = arcpy.SearchCursor(alayer,"", sort_fields="TFLAG A")  FList = [] AList = []  for row in FRows:     Fvalue = row.getValue("FLAG")     FList.append(str(Fvalue))  for rows in ARows:     Avalue = row.getValue("TFLAG")     AList.append(str(Avalue))  matched = set(FList) & set(AList)  for x in matched:     exp = "ID = " + x     arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)     arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION") 
Tags (2)
0 Kudos
17 Replies
ChrisSnyder
Honored Contributor
import arcpy
flayer = "AADT"
alayer = "AADTAnnoLabel"


Seems like you need to better define the paths to your inputs. Also, are you sure the fields you are attempting to sort exist in the input tables? Are you sure these are the actual field names and not just aliases?
0 Kudos
MathewCoyle
Honored Contributor
OK, when I change my code to work for the traditional cursors I get the lovely Error: 999999: Error executing function. Which from what i get from other threads in the forum, this is not good. Iam running it in the python window inside the mxd could that be the problem?

import arcpy

flayer = "AADT"
alayer = "AADTAnnoLabel"

FRows = arcpy.SearchCursor(flayer,"","","","FLAG")
ARows = arcpy.SearchCursor(alayer,"","","","TFLAG")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("FLAG")
    FList.append(str(Fvalue))

for rows in ARows:
    Avalue = row.getValue("TFLAG")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = "ID = " + x
    arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)
    arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION")
Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.



Is the error coming from your search cursor line? If so the most likely cause of a 999999 error is that the fields you are trying to sort by do not exist in that table.
0 Kudos
MichelleCouden1
Deactivated User
Your good Matt!! Yeah, it's coming from the search cursor line. I did talk to some guys across the hallway and they said just make it a stand alone script in PythonWin because the search cursor commands inside the ArcMAP python window are different. I've only set parameters at the beginning of a code. What would I put at the begnning of this code to make it work as a script tool in ArcMAP without parameters. For Example: He would just open or start the script from the toolbox and it would work that mxd.
0 Kudos
MichelleCouden1
Deactivated User
Would I do an mxd = arcpy.mapping.MapDocument..... and then a lstLayers. In order for it to be a stand alone script that I could move into a toolbox.  See code below:

import arcpy

mxd = arcpy.mapping.MapDocument(r"K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\District_Maps\2012\Abilene")
lstLayers = arcpy.mapping.ListLayers(mxd)

flayer = "AADT"
alayer = "AADTAnnoLabel"

FRows = arcpy.SearchCursor(flayer,"","","","FLAG")
ARows = arcpy.SearchCursor(alayer,"","","","TFLAG")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("FLAG")
    FList.append(str(Fvalue))

for rows in ARows:
    Avalue = row.getValue("TFLAG")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = "ID = " + x
    arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)
    arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION")
0 Kudos
MathewCoyle
Honored Contributor
Yes you can do that. You would just need to reference the layer names as layer objects in the mxd.

eg
flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0]
alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]
0 Kudos
MichelleCouden1
Deactivated User
Thank You Matt for all your help!! You to csny490!! You guys are the best! I really appreciate all your help!!
0 Kudos
MichelleCouden1
Deactivated User
I am getting an exception raised on the SearchCursor. So wouldn't I change the Search Cursor code to, look at the layer and then add a ListFields line of code like what I have below:

import arcpy

mxd = arcpy.mapping.MapDocument(r"K:\TASS\2 - GEO-DATA PROCESSING SUPPORT\MICHELLE'S WORK_ENTER NOT!!\Work Folder\Python Programming\Wayne's Tools\Abilene_Base_Map.mxd")
lstLayers = arcpy.mapping.ListLayers(mxd)

flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0]
alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]

FRows = arcpy.SearchCursor(flayer)
ARows = arcpy.SearchCursor(alayer)

fields = arcpy.ListFields(mxd, "", "FLAG")

FList = []
AList = []
0 Kudos
MichelleCouden1
Deactivated User
I am getting an exception raised on my for row lines. I am thinking it is because I have notdeclared the field names correctly. Would I use the ListFields command line or something else.

import arcpy  mxd = arcpy.mapping.MapDocument(r"K:\TASS\2 - GEO-DATA PROCESSING SUPPORT\MICHELLE'S WORK_ENTER NOT!!\Work Folder\Python Programming\Wayne's Tools\Abilene_Base_Map.mxd") lstLayers = arcpy.mapping.ListLayers(mxd)  flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0] alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]  FRows = arcpy.SearchCursor(flayer) ARows = arcpy.SearchCursor(alayer)  #ffields = arcpy.ListFields(mxd, "", "FLAG") #afields = arcpy.ListFields(mxd, "", "TFLAG")  FList = [] AList = []  for row in FRows:     Fvalue = row.getValue("FLAG")     FList.append(str(Fvalue))  for rows in ARows:     Avalue = row.getValue("TFLAG")     AList.append(str(Avalue))  matched = set(FList) & set(AList)  for x in matched:     exp = "ID = " + x     arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)     arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION") 
0 Kudos