This is my first whack at field mapping. I've looked through the history of posts on this issue and have implemented the solutions (as far as I understand them). But it's not working for me.
Basically, when I assign field = fieldMap.outputField, the field variable then references a describe field object object instead of a field object. I can field.name = "NAME" but when I apply that to the field map via fieldMap.outputField(file, field) it bombs.
It's my understanding that fieldMap.outputField should return a field object, but for some reason I'm getting a describe object. Any help would be welcome!
# input files and workspaces
fgdb = r"N:\GDB\GDB.gdb"
HDMSfile = r"N:\GDB\plants\HDMS.shp"
GDBfile = r"N:\GDB\GDB.gdb\Occurrence_scriptgen"
sppfile = r"N:\GDB\GDB.gdb\SpeciesList"
# environments set
arcpy.env.workspace = fgdb
# field map preparation
fieldmappings = arcpy.FieldMappings()
fm_SiteIdHDMS = arcpy.FieldMap()
print "Field map prepared."
fm_SiteIdHDMS.addInputField(HDMSfile, "EO_ID")
fld_SiteIdHDMS = fm_SiteIdHDMS.outputField
fld_SiteIdHDMS.name = "SiteID_HDMS"
print fld_SiteIdHDMS.name
print fld_SiteIdHDMS
fm_SiteIdHDMS.outputField(GDBfile, fld_SiteIdHDMS)
Python Interpreter Results:
Field map prepared.
SiteID_HDMS
<geoprocessing describe field object object at 0x16A18F80>
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "N:\RarePlants_CNF\PythonScripts\HDMS_to_RarePlantOccurrence.py", line 46, in <module>
fm_SiteIdHDMS.outputField(CNFfile, fld_SiteIdHDMS)
TypeError: 'Field' object is not callable
(line 46 corresponds to line 20 of the above scrippet)
Solved! Go to Solution.
outputField is a property that returns an object (Field object) that isn't callable. Line 20 is throwing the error. If you are trying to set the property, you need to get the property and change it and then set it back.
fld = fm_SiteIdHDMS.outputField fld.name = "Whatever" fm_SiteIdHDMS.outputField = fld
outputField is a property that returns an object (Field object) that isn't callable. Line 20 is throwing the error. If you are trying to set the property, you need to get the property and change it and then set it back.
fld = fm_SiteIdHDMS.outputField fld.name = "Whatever" fm_SiteIdHDMS.outputField = fld
So it is. I completely missed that.
Thank you.
These three lines of code have me confused ... specifically the last line:
fm_SiteIdHDMS.output = fld
The first two lines make sense. Set the output field name to "Whatever". However, once the property is set, why does it need to be changed back? Isn't this undoing what was just done?
Also, could the first two lines simply be changed to:
fm_SiteIdHDMS.outputField.name = "Whatever"
Is this stylistic or is there a Pythonic/logical reason for the "fld" alias? (I find the latter hard to read.)
I did notice a typo on Line 3 of my original code, so I went back and fixed it. Thanks.
My answer to your question is touched on in my response to the OP. AFAIK, properties of ArcPy objects cannot be modified in place. Instead, users must get the property, modify the returned object, and then set the property. Trying what you suggest:
>>> #Create temporary, in-memory feature class >>> fc = arcpy.CreateFeatureclass_management('in_memory','fc',"POINT") >>> arcpy.AddField_management(fc, "Example","TEXT") <Result 'in_memory\\fc'> >>> #Create FieldMap object and add input field >>> fm = arcpy.FieldMap() >>> fm.addInputField(fc, "Example") >>> fm.outputField.name u'Example' >>> #Try to change field name in place >>> fm.outputField.name = "Whatever" >>> fm.outputField.name u'Example' >>> #Change field name by getting, modifying, and setting Field object >>> fld = fm.outputField >>> fld.name = "Whatever" >>> fm.outputField = fld >>> fm.outputField.name u'Whatever'
To footnote my own comment above, properties can be set directly if a user can construct or construct and modify the correct object without getting the property first. For example, properties that are strings can be changed by setting the property with a new string:
>>> fld = arcpy.Field() >>> fld.name u'' >>> fld.name = "Whatever" >>> fld.name u'Whatever'