I am getting a "name 'row' is not defined" error when I run a particular piece of code (see below). This only happens when my input layer has one entry instead of two. So when the attribute query is run to find a second row there is no row to find and hence the error appears.
The aim of my overall script mean this will happen from time to time, what I am looking for is a way to accommodate this error and either avoid this section of script when an error is likely to occur or continue with the script even if this error pops up. I have tried to incorporate a 'try' and 'except' statement into the loop but have been unsuccessful. Would this work? if so how? If anyone has an idea on how to get around this problem please let me know.
import arcpy
from arcpy import env
env.workspace = "C:\MyArcGIS\EDRN_Shp"
arcpy.env.overwriteOutput = True
join110 = "C:\\MyArcGIS\\EDRN_Shp\\join110.shp"
joinField = "LCC_DRN_ID"
query1 ='"FID" = 1'
arcpy.MakeFeatureLayer_management(join110, "feature_layer12")
arcpy.SelectLayerByAttribute_management ("feature_layer12", "NEW_SELECTION", query1)
rows = arcpy.SearchCursor("feature_layer12")
for row in rows:
if row.getValue("edrn_in") >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_in")
if row.getValue("edrn_out") >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_out")
del row, rows
-
Do you have a version of the try/except you used that didn't work?Erica– Erica2014年08月14日 17:08:26 +00:00Commented Aug 14, 2014 at 17:08
-
what happens if you change the second if statement to elif...user681– user6812014年08月14日 17:49:08 +00:00Commented Aug 14, 2014 at 17:49
-
Originally I used elif instead of if, that didn't work either. I'll post my attempt at a try/except tomorrow when I'm next at my computer Erica.Geord359– Geord3592014年08月14日 17:54:02 +00:00Commented Aug 14, 2014 at 17:54
-
I think that if you just remove "del row", the problem should disappear. More importantly, Your code runs the same "joinfield" for every row in your database. I would recommend to count the number of feature matching your query, not to do this within a cursor.radouxju– radouxju2014年08月14日 21:36:19 +00:00Commented Aug 14, 2014 at 21:36
1 Answer 1
I would try using with
instead, and also for performance switching to da
search cursors
import arcpy
from arcpy import env
env.workspace = "C:\MyArcGIS\EDRN_Shp"
arcpy.env.overwriteOutput = True
join110 = "C:\\MyArcGIS\\EDRN_Shp\\join110.shp"
joinField = "LCC_DRN_ID"
fc = r"C:\Shapefile\featurelayer12.shp"
fields = ["erdn_in", "erdn_out"]
query1 ='"FID" = 1'
arcpy.MakeFeatureLayer_management(join110,fc)
arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", query1)
with arcpy.da.SearchCursor (fc, fields) as cursor:
for row in cursor:
if row[0] >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_in")
elif row[1] >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_out")
else:
pass
This should work in 10, what I think was happening is you didn't initially define your field names, therefore getValue
wouldn't work.
import arcpy
from arcpy import env
env.workspace = "C:\MyArcGIS\EDRN_Shp"
arcpy.env.overwriteOutput = True
join110 = "C:\\MyArcGIS\\EDRN_Shp\\join110.shp"
joinField = "LCC_DRN_ID"
fc = r"C:\Shapefile\featurelayer12.shp"
erdn_in = "erdn_in"
erdn_out = "erdn_out"
query1 ='"FID" = 1'
arcpy.MakeFeatureLayer_management(join110,fc)
arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", query1)
rows = arcpy.SearchCursor(fc)
row = rows.next()
while row:
if row.getValue(erdn_in) >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_in")
elif row.getValue(erdn_out) >= "E":
arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_out")
else:
pass
row = rows.next()
-
Thanks GISKid, I can't use the da search cursor though as I am restricted to using ArcGIS 10. Will I be able to use something similar in 10?Geord359– Geord3592014年08月14日 17:49:47 +00:00Commented Aug 14, 2014 at 17:49
-
You need a
for row in cursor:
in that code.nmpeterson– nmpeterson2014年08月14日 18:18:26 +00:00Commented Aug 14, 2014 at 18:18 -
Thanks @nmpeterson, overlooked that. I've changed the code to 10, you could use a
for
loop as wellGISHuman– GISHuman2014年08月14日 19:23:20 +00:00Commented Aug 14, 2014 at 19:23 -
Awesome, glad to hear it!:)GISHuman– GISHuman2014年08月15日 14:27:52 +00:00Commented Aug 15, 2014 at 14:27