I cannot seem to get SelectLayerAttribute to work using Python?

1591
8
Jump to solution
11-20-2019 01:39 PM
Mark_Hotz
Occasional Contributor II

I haven't written a Python script in years...so this is my first one in a LONG time.  With that in mind the issue could be more than one issue...I'm not sure.

I have a Python script almost working...just one more hurdle to complete and I'm ready to move on.  I'm running this script in ArcGIS Pro 2.4.2.  There is a sample feature class with 100 points, plus I created a list to extract values that will be passed to a "calc" function later.  The "list" I created was made using the Summary function in ArcGIS Pro, and I employ a cursor function as per below.  It too works really well and does what it's supposed to do.

However, this issue has me a little stumped, but I'm sure it's relatively easy to resolve...it could be a few different things, or it could be that Python isn't even capable to doing it they way I'm doing it.

The script does everything I want it to do except select the specific records in an attribute table.  I also have a CLEAR_SELECTION process that also runs immediately after this SelectLayerByAttribute string, but I don't know if it's doing anything either.  If I do this select manually it works without issue, but in a Python script it seems to have an issue.  Rather than select the specific group of records based on a unique identifier (MMSI) this SelectLayerByAttribute process selects the entire table for each iteration of the loop.  So when I calculate the field (in the next step) it just overwrites everything that was calculated in the previous iteration each time.  This process should update all of the blank fields/records found in the attribute table...and it too also works well manually.

So can anyone see what the issue might be here?  The code appears to be correct, so the does the syntax...what am I not seeing that will make this work properly?

Thanks

0 Kudos
1 Solution

Accepted Solutions
RemigijusPankevičius
New Contributor III

You better post code snippet, not an image.

I see that you ignore whatever result you get from SelectLayerByAttribute_management

and later you do GetCount_Management on fc1 which is feature class string and it has not changed afyer your invoked SelectLayerByAttribute_management. Therefore you get count of all features in that feature class.

You should get both filtered layer and count (I guess a pair) as a result from SelectLayerByAttribute_management, so you don't need to call GetCount_Management at all, see Derived Output topic here:

Select Layer By Attribute—Data Management toolbox | ArcGIS Desktop 

Here is my experiment with IDLE in ArcMap 10.5

>>>import arcpy
>>> featureClass = r'C:\DATA\Test-Attach\152287.gdb\Markanordningslinje'
>>> arcpy.GetCount_management(featureClass)
<Result '11673'>


# I need to do this step in ArcMap, but you probably don't need it in Pro:
>>> arcpy.MakeFeatureLayer_management(featureClass, "lyr")
<Result 'lyr'>


# Now in ArcMap I have to do 2 steps:
>>> res = arcpy.SelectLayerByAttribute_management("lyr", 'NEW_SELECTION', "DTYPE='Stenmur'")
>>> arcpy.GetCount_management(res)
<Result '3267'>

#.. but I think in Pro you can do like this and get count directly:
out_layer, count = arcpy.SelectLayerByAttribute_management("lyr", 'NEW_SELECTION', "DTYPE='Stenmur'")

View solution in original post

8 Replies
FC_Basson
MVP Regular Contributor

You can use the AddFieldDelimeters function to rewrite a more suitable SQL query for the SelectByAttribute:

sql = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(fcl,"MMSI"), recordl[l])
arcpy.SelectLayerByAttribute_management(fcl,"NEW_SELECTION", sql )‍‍
Mark_Hotz
Occasional Contributor II

FC Basson:

Thanks for the tip.  I will explore this further and hopefully improve this current script (and future code too).  I have a few things to learn still, but I believe I'm making progress too.  There's writing code, and then there's writing code properly, so I'm always striving to improve.

Thanks again.

0 Kudos
LukeWebb
Occasional Contributor III

Your code looks good. You mention other stuff also happens, perhaps your error is somehow there!

You could try defining a feature layer in the script to see if that helps. (If you are running from desktop, it shouldn't make a difference)

Make Feature Layer—Data Management toolbox | ArcGIS Desktop 

Mark_Hotz
Occasional Contributor II

Luke:

Thanks for the response.  It's always great when the community can help out.  I did narrow down the problem to that one specific Select command...but I still couldn't see exactly where the issue is.  However, as per below, a resolution was found.  You're right though...the code was good...except for one small oversight :-).  The script does indeed work now (and does exactly what I wanted it to do)...and that marks the first time in years where I have successfully written a arcpy script in ArcGIS (ArcGIS Pro in my case). This one script will save me days of work every time I need to run it (the manual alternative is both tedious and time consuming.  

Thanks again

0 Kudos
RemigijusPankevičius
New Contributor III

You better post code snippet, not an image.

I see that you ignore whatever result you get from SelectLayerByAttribute_management

and later you do GetCount_Management on fc1 which is feature class string and it has not changed afyer your invoked SelectLayerByAttribute_management. Therefore you get count of all features in that feature class.

You should get both filtered layer and count (I guess a pair) as a result from SelectLayerByAttribute_management, so you don't need to call GetCount_Management at all, see Derived Output topic here:

Select Layer By Attribute—Data Management toolbox | ArcGIS Desktop 

Here is my experiment with IDLE in ArcMap 10.5

>>>import arcpy
>>> featureClass = r'C:\DATA\Test-Attach\152287.gdb\Markanordningslinje'
>>> arcpy.GetCount_management(featureClass)
<Result '11673'>


# I need to do this step in ArcMap, but you probably don't need it in Pro:
>>> arcpy.MakeFeatureLayer_management(featureClass, "lyr")
<Result 'lyr'>


# Now in ArcMap I have to do 2 steps:
>>> res = arcpy.SelectLayerByAttribute_management("lyr", 'NEW_SELECTION', "DTYPE='Stenmur'")
>>> arcpy.GetCount_management(res)
<Result '3267'>

#.. but I think in Pro you can do like this and get count directly:
out_layer, count = arcpy.SelectLayerByAttribute_management("lyr", 'NEW_SELECTION', "DTYPE='Stenmur'")
Mark_Hotz
Occasional Contributor II

Remigijus:

Thanks for the feedback...it does help a lot and it's most appreciated.  I have never posted a snippet of code here before...so I will have to explore that further to see how it's done, and then use that next time.

I see where I made my mistake now.  I didn't pass the results of my select to a variable and then pass that to my calculate field command, nor did I pass the select result variable to my GetCount command.  I was probably thinking like I do when I do this manually using the Select tool and just assumed the results would magically happen :-).  

I made the adjustments and the script now works perfectly.

Thanks again.

0 Kudos
Mark_Hotz
Occasional Contributor II

Problem solved:

My coding was "almost" right LOL.

I had neglected to add a variable to my SelecdtLayerByAttribute command, and then use that to display (print) how many records were selected and to then calculate the field.  I think I just thought that once selected then (just like it happens in ArcGIS/ArcGIS Pro) it automatically knows what I would have selected.  Now this works exactly how it should work.  It's slow, but it's going to run after midnight anyway, so it really doesn't matter.

Thanks everyone for their help though...everything helped steer me in the right direction.

0 Kudos
RemigijusPankevičius
New Contributor III

I think this will work for pasting code...How to quote code in geonet

0 Kudos