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

1597
17
Jump to solution
04-22-2013 06:01 AM
MichelleCouden1
Occasional Contributor III
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
1 Solution

Accepted Solutions
MichelleCouden1
Occasional Contributor III
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") 

View solution in original post

0 Kudos
17 Replies
ChrisSnyder
Regular Contributor III
I'm not sure if the traditional (non data access) cursors support named parameters (which is what you are doing with the 'sort_fields' named parameter in your code).

I think it would either have to be like this:

In traditional cursors:
FRows = arcpy.SearchCursor(flayer,"", "", "", "FLAG A")


Or in data access cursors like this:
FRows = arcpy.da.SearchCursor(flayer,["*"], sql_clause=(None, 'ORDER BY FLAG'))

This thread might help: http://forums.arcgis.com/threads/64580-SQL-sort-parameter-syntax-for-da.cursors
0 Kudos
MathewCoyle
Frequent Contributor
As of 10.1 all arcpy tools should support kwargs for parameters. 10.0 they did not.
0 Kudos
ChrisSnyder
Regular Contributor III
Upon testing in v10.1, indeed the 'sort_fields' named parameter/kwarg appears to work as advertised.
0 Kudos
MichelleCouden1
Occasional Contributor III
OK, ran the first code for traditional cursors and got a runtime error of error:999999. Saw that error on the forum but no one answered the poor guy, so I am not sure what that error means. Tried the second code for data access cursors and got the error 'module' object has no attribute 'da'. I'm thinking that means I just need to declare the object maybe??

import arcpy

flayer = "AADT"
alayer = "AADTAnnoLabel"

FRows = arcpy.da.SearchCursor(flayer,["*"],sql_clause=(None, 'ORDER BY FLAG'))
ARows = arcpy.da.SearchCursor(alayer,["*"],sql_clause=(None, 'ORDER BY 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
Frequent Contributor
It sounds like you are using ArcGIS 10.0 not 10.1.
0 Kudos
MichelleCouden1
Occasional Contributor III
Yep, you are correct! I am sorry I did not check. I forgot I work for the state. They don't believe in current. So from the previous threads, I should be using I can use the sort fields I just have it coded wrong correct??? I am sorry this version stuff is hard for me to get.
0 Kudos
ChrisSnyder
Regular Contributor III
Use the "traditional cursor method" shown here: http://forums.arcgis.com/threads/82818-quot-Search-Cursor()-got-an-unexpected-keyword-argument-sort_...

Since you are using v10.0, you cannot use the named parameter feature.

Upgrade! v10.2 will be out in June...
0 Kudos
MichelleCouden1
Occasional Contributor III
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.

0 Kudos
MichelleCouden1
Occasional Contributor III
I think I would rather it start in the PythonWin window instead of the window in ArcMAP. Because I did learn there are other codes for searching. I need help at the start of the code to make that work, because I've only written codes with parameters being set and this one doesn't need it.
0 Kudos