Select unique values from field using selectbyattribute

5320
16
03-11-2018 06:51 PM
wwnde
by
Occasional Contributor

I am a vety new to python. I have done the script below which identifies unique values and lists them. That goes ok. The next thing, I want it to iterate through the filed LinkKey as it selects similar LinkKeys and saves them in a work space.

All i get is a message saying zero records selected. I suspect the issue is in my whereclause.

Any help?

Tags (1)
0 Kudos
16 Replies
RandyBurton
MVP Alum

It looks like you are comparing LinkKey (or "field") to a text value instead of a number in your whereclause. Try:

# omit the single quotes around the second %s

whereclause = "%s = %s" % ('LinkKey', x)

# optional format

whereclause = "{} = {}".format('LinkKey', x)
wwnde
by
Occasional Contributor

Thanks Randy, done that but it doesnt work it  returns

ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

0 Kudos
DanPatterson_Retired
MVP Emeritus

William... do the selectbyattributes manually, go to the Results window, and copy the python snippet.

You will get the correct syntax for the where clause.  While you are doing the test, do one query for a numeric field and one for a text field and save the examples, so the next time you try to shortcut by writing the syntax out, you will have a guideline to follow

ie 

# ---- SBA for text
arcpy.management.SelectLayerByAttribute("sample_1000", "NEW_SELECTION", "Facility = 'Hall'", None)

# ---- SBA for numbers
arcpy.management.SelectLayerByAttribute("sample_1000", "NEW_SELECTION", "Time = 1", None)
wwnde
by
Occasional Contributor

Thanks Dan, Thats why I did first, it worked for individual records,. I have no issue selecting an individual item in this case a specific LinkKey, the issue is when I want it to iterate and pick similar sets of duplicated 'LinkKeys'. Say if there  is a  dozen set of  'p' link keys and half a dozen 'j' link keys, I want it to select p, save and proceed to select j.....

0 Kudos
DanPatterson_Retired
MVP Emeritus

you build your 'where clause outside

for i in vals:
    exp = "\"Facility = '{}'\"".format(i)  # build your expression
    print(exp)                             # and substitute it in SBA
    
"Facility = 'Hall'"
"Facility = 'Church'"
"Facility = 'Hospital'"
wwnde
by
Occasional Contributor

Thanks Dan, as mentioned earlier, I am only a toddler and only flowing through logic. I have put the script as below and it returns error, 

"Traceback (most recent call last):
File "C:\avhome\Aftertraining Scripts\topology24.py", line 26, in <module>
for i in Vals:
TypeError: 'NoneType' object is not iterable"

Didnt I define Vals as is suppossed to be?

0 Kudos
DanPatterson_Retired
MVP Emeritus

You really need to thrown some print statements into your script so you can see what you got.

I bet if you check your unique values list, there is a None in there.

Better still check for None

a = ['A', None, 'B']

for i in a:
    if i is not None:
        print('good entry')
    else:
        print('got a {}'.format(str(i)))
        
good entry
got a None
good entry

and if you get a 'bad' entry (ie None), then skip the selectbyattributes

JoeBorgione
MVP Emeritus

I'm just a big toddler in python, but after looking at your code, I tried a slightly different approach.  I get confused with counter variables and avoid them by stepping through a list based on the length of the list:

fc = 'PLOTS_Test'
arcpy.MakeFeatureLayer_management(fc,'Test_lyr')
field_list = []
with arcpy.da.SearchCursor( fc, 'LinkKey') as cursor:
    for row in cursor:
        field_list.append(row[0])

for i in range(len(field_list)):
    where= 'LinkKey = {}'.format(field_list)
    arcpy.SelectLayerByAttribute_management('Test_lyr','ADD_TO_SELECTION',where)
print 'Records Selected = {}'.format(arcpy.GetCount_management('Test_lyr'))
arcpy.CopyFeatures_management('Test_lyr','sasal')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I took out kala, as I'm not sure you actually need it, and just work directly with the feature layer from the feature class. 

edited moments later:  sometimes the GetCount_management can throw you a curve.  It honors the selection and will return the number selected, IF there is a selected set.  If nothing is selected, you get the count of all the records.  A way around that is using arcpy.Describe, which doesn't give you a number of selected records, but can tell you if you have selected records.  I've used it this way:

if arcpy.Describe("view").FIDSet =='':
  pass
elif int(arcpy.Describe("view").FIDSet[0]) >= 1:
  #do stuff here...‍‍‍‍

and edited a little later after that....

I took a look at get list of highlighted features within a selection  and the response from Richard Fairhurst provided a little inspiration to get the number of selected records:

if int(arcpy.Describe("view").FIDSet[0]) >= 1:
   i = 0
   rows = arcpy.da.SearchCursor("view","*")
   for row in rows:
      i = i+1
        
print 'Rows selected = {}'.format(i)
That should just about do it....
wwnde
by
Occasional Contributor

Joe, very useful. it worked, importantly it introduced me to the  arcpy.Describe. Thanks

0 Kudos