Using a search Cursor I keep getting an error

1198
4
Jump to solution
02-02-2017 05:23 PM
Leith_JohnHAWKINS
New Contributor III

Hi ,

So I am using a search cursor to run through each row and multi buffer  - having a few dramas and I cant seem to get my head around it!

I assume its to do with how I am calling row into the buffer ?

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

file_workspace = "r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb"
env.workspace = file_workspace

Holdings = r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb\Data\Holdings'
ofc = r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb'
distances = [1000, 4000]
unit = "Meters"
arcpy.MakeFeatureLayer_management(Holdings,'Holdings_Layer')

with arcpy.da.SearchCursor(Holdings, ['Holding_Reference_Number'])as Holdings_Ref_cursor:
    for x in Holdings_Ref_cursor:
        print x[0]
        Poyl_name = row[0]
        arcpy.SelectLayerByAttribute_management('Holdings_Layer', 'NEW_SELECTION', '"' + Holdings + '" = \'' + Poyl_name + '\'')
        arcpy.MultipleRingBuffer_analysis('Holdings_Layer', 'ofc', distances, unit, "", "ALL")
       #arcpy.Buffer_analysis("Holdings_Layer", ofc, var_Buffer, "FULL", "ROUND", "ALL", "")
        print 'Buffer complete'
        print('Buffer Complete')


Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

That's because you've called 'x' what most examples call 'row' (it would normally be more like "for row in cursor:"). It doesn't matter, it's just a variable name, but if you call it 'x', then you need to reference 'x' (i.e. x[0] is the Holding_Reference_Number). Or, you can't set x[0] to row[0], because row doesn't exist. Also, you've got a new error coming up in the select - there's an extra apostrophe right after x[0].

View solution in original post

4 Replies
DarrenWiens2
MVP Honored Contributor

What's the error message?

As it is now, it looks like the line Poyl_name = row[0] is commented out (although I don't see the '#'), so there is no variable called Poyl_name. If it's not commented out, then it's trying to use row[0], while you've already called it x[0].

0 Kudos
Leith_JohnHAWKINS
New Contributor III

Sorry I forgot to add the error.

So I have made some changes but still getting that the row is not defined ..

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

file_workspace = "r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb"
env.workspace = file_workspace

Holdings = r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb\Data\Holdings'
ofc = r'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb'
distances = [1000, 4000]
unit = "Meters"
arcpy.MakeFeatureLayer_management(Holdings,'Holdings_Layer')

with arcpy.da.SearchCursor(Holdings, ['Holding_Reference_Number'])as Holdings_Ref_cursor:
    for x in Holdings_Ref_cursor:
        print x[0]
        x[0] = row[0]
        arcpy.SelectLayerByAttribute_management('Holdings_Layer', 'NEW_SELECTION', '"' + Holdings + '" = \' + x[0]' + '\'')
        arcpy.MultipleRingBuffer_analysis('Holdings_Layer', 'ofc', distances, unit, "", "ALL")
       #arcpy.Buffer_analysis("Holdings_Layer", ofc, var_Buffer, "FULL", "ROUND", "ALL", "")
        print 'Buffer complete'
        print('Buffer Complete')
0 Kudos
DarrenWiens2
MVP Honored Contributor

That's because you've called 'x' what most examples call 'row' (it would normally be more like "for row in cursor:"). It doesn't matter, it's just a variable name, but if you call it 'x', then you need to reference 'x' (i.e. x[0] is the Holding_Reference_Number). Or, you can't set x[0] to row[0], because row doesn't exist. Also, you've got a new error coming up in the select - there's an extra apostrophe right after x[0].

Leith_JohnHAWKINS
New Contributor III

Thanks makes sense ill implement

0 Kudos