Select to view content in your preferred language

Why sql_clause is not respected in da.SearchCursor?

3277
21
Jump to solution
11-19-2017 11:45 AM
AdamBorczyk
Regular Contributor

I have a shapefile where I have polylines and their fields are:

- length

- area into which they fall in

Now I need 3 longest lines from each area. I have 3 areas and 50 polylines, so I should get 3*3=9 records as a result. What I try to do is:

import arcpy

myFile = somePath
sql = ('TOP 3', 'ORDER BY "length" DESC GROUP BY "area_id" ')

cursor = arcpy.da.SearchCursor(myFile , "*", None, None, False, sql)

with cursor:
    for r in cursor:
        print r‍‍‍‍‍‍‍

I get all of 50 records here. The same happens when I set `sql` to just `('TOP 1', None) or anything else.

Is the syntax wrong here?

0 Kudos
21 Replies
JamesCrandall
MVP Frequent Contributor

I get 'IndexError: list assignment index out of range' on this line:

selections[row[0]][row[1]] = row[2]
0 Kudos
AdamBorczyk
Regular Contributor

Since selections is defaultdict(dict), I think the error can appear only because there is no row[0], row[1] or row[2]. Please try to print it and see it you get correct results from cursor.

0 Kudos