FieldMap API : Strange Behavior. Can someone explain?

845
5
02-23-2023 07:13 AM
Labels (1)
RobertStevens
Occasional Contributor III

I am using arcpro3.0.3 and am using the TableToTable API to convert a shapefile .dbf file into .csv.

One of the arguments to TableToTable is a FieldMappings parameters, and I amusing this to rename all of the fields in the original shapefile.

The documentation of FieldMap has example code which looks like this (names of variables not the same as in documentation, but you get the idea)

fm = arcpy.FieldMap()
fm.addInputField(tablename,oldfieldname)
outf = fm.outputField
outf.name = newfieldname
fm.outputField = out
...

The above code seems to work, but this code below  (which appears to me to have the same effect does not)
The  code which differs in the two examples is in red

fm = arcpy.FieldMap()
fm.addInputField(tablename,oldfieldname)
fm.outputField.name = newfieldname
...

In the first case the field in question has its  name in the output .csv file  changed (from oldfieldname to newfieldname; in the second case the field name remains unchanged. Why is that? To my eye both code snippets do exactly the same thing.

Tags (2)
0 Kudos
5 Replies
Kepa
by Esri Contributor
Esri Contributor

Hello @RobertStevens,

The same happened to me first time I had to use FieldMapping on a script. I don't really know how FielMap object works behind the scenes but it seems that you cannot set name property directly unless you declare a new variable in memory for outputField property.

It's an odd behaviour, but somehow it could be related to the ArcObjectToPythonObject conversion. Digging into FieldMap class, outputField is set with passthrough_attr function defined on _base.py module. And from there it becomes complex enough to lose track of what it is being done.

Hope somebody else could shed some light on this particular behaviour.

Regards,

0 Kudos
RobertStevens
Occasional Contributor III

Hi Kepa

Thanks for your reply. I was wondering whether I could believe my own eyes. The examples in ESRI's documentation use that rather long winded way of creating a fieldmap object. One would think that ESRI might perhaps explain why it is necessary to do it that way. I don;t know why it is with this software that there is always some surprise or undocumeted strangeness lurking around the corner. Like you, I am hoping someone else with more knowledge of ESRI's Python maze can comment.

0 Kudos
RobertStevens
Occasional Contributor III

Follow up to original posting: I found this baffling and filed it as a bug. ESRI have confirmed that this behavior of the API is incorrect and  new bug has been opened against it. In my bug report I mused that possibly many other APIs might have similar unusual behavior, but that i just speculation on my part. Whatever causes this bug it seems to me inconsistent with Python syntax and semantics. It is almost as thought the equality operator has been redefined as is possible in C++.

ESRI have reproduced this behavior and filed as a bug: BUG-000156266

0 Kudos
Tomasz_Tarchalski
New Contributor III

Hello, 

what if you convert fm to string

fm = str(fm). I have noticed that since arcgis pro 3.X i have to add that line  (converting your field mapping to string)....

0 Kudos
RobertStevens
Occasional Contributor III

Hello Tomaxz

I haven't tried, and will when I have time.

But if you convert to a string how can it have  method addInputField ?
I am confused.

0 Kudos