I've got 19 feature classes, with a single field in each one that I am interested in. I want to pull that field from each one (index 63 on each feature class), and join those fields to a single feature class (inDWR). In the workspace, I've got numerous fc's, and the 19 aforementioned feature classes all end with 'EVTP' in their file name. So, I've distilled a list accordingly (extractList):
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = 'C:\\LaPlata\\LaPlataFmTops.gdb'
inDWR = env.workspace + '\\DWR_ConstructedAbandonedReplaced'
tempList = arcpy.ListFeatureClasses() # This contains too many files, some of which I'm not interested in
extractList = []
for fc in tempList:
if fc.endswith ('EVTP'):
extractList.append(fc)
print (extractList) #contains only the fc's I'm interested in
for fc in extractList:
fieldList = arcpy.ListFields(fc)
joinField = fieldList[4] #this is == 'Permit', and is the join field for the input table and the fc
fieldValue = fieldList[63] #field I want to join from each fc to inDWR
arcpy.JoinField_management (inDWR, joinField, fc, joinField, fieldValue)
I keep getting this error:
Runtime error
Traceback (most recent call last):
File "<string>", line 6, in <module> #This would be the arcpy.JoinField_management line
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 6593, in JoinField
raise e
RuntimeError: Object: Error in executing tool
1 Answer 1
I suspect your problem is:
fieldList = arcpy.ListFields(fc)
joinField = fieldList[4]
fieldValue = fieldList[63]
joinField
and fieldValue
will be field objects (look a code sample below help text) not field names which join field wants.
Extract name by:
joinField = fieldList[4].name
fieldValue = fieldList[63].name
In my experice Join Field can be very slow. I would create a dictionary using da.SearchCursor and then update your fc with da.UpdateCursor.
Untested:
import arcpy, os
arcpy.env.workspace = r'C:\LaPlata\LaPlataFmTops.gdb'
inDWR = os.path.join(env.workspace, 'DWR_ConstructedAbandonedReplaced')
extractlist = [f for f in arcpy.ListFeatureClasses() if f.endswith('EVTP')]
for fc in extractList:
fieldList = arcpy.ListFields(fc)
joinField = fieldList[4] #Now i want Field object to later extract name, type etc. from
fieldValue = fieldList[63]
d = {joinField:fieldValue for joinField,fieldValue in arcpy.da.SearchCursor(fc,[joinField.name,fieldValue.name])}
arcpy.AddField_management(in_table=inDWR, field_name=fieldValue.name, field_type=fieldValue.type) #You might also need a field_length=fieldValue.length if you have a text field.
with arcpy.da.UpdateCursor(inDWR, [joinField.name,fieldValue.name]) in cursor:
for row in cursor:
if row[0] in d:
row[1] = d[row[0]] #Fetch value from dictionary
cursor.updateRow(row)
else:
#row[1] = 'MISSING'
pass #Or just skip it
Explore related questions
See similar questions with these tags.