1

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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 14, 2014 at 17:06
4
  • Do you have a version of the try/except you used that didn't work? Commented Aug 14, 2014 at 17:08
  • what happens if you change the second if statement to elif... Commented 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. Commented 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. Commented Aug 14, 2014 at 21:36

1 Answer 1

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()
answered Aug 14, 2014 at 17:33
4
  • 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? Commented Aug 14, 2014 at 17:49
  • You need a for row in cursor: in that code. Commented Aug 14, 2014 at 18:18
  • Thanks @nmpeterson, overlooked that. I've changed the code to 10, you could use a for loop as well Commented Aug 14, 2014 at 19:23
  • Awesome, glad to hear it!:) Commented Aug 15, 2014 at 14:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.