Select to view content in your preferred language

FieldMappings: Error in removing a field map from field mappings for RemoveFieldMap

3820
6
Jump to solution
01-26-2016 01:21 AM
ZdeněkSoldán
Regular Contributor

Hi

I have a layer named patro_join with 114 fields and I want to preserve only two fields ("Layer","Text") in this layer. I tried to use FieldMappings but it removed only a half of fields and 57 fields still stay in the layer. I don´t know what is wrong. Here is my part of code:

fieldmappings = arcpy.FieldMappings()

fieldmappings.addTable("patro_join")

i=0

for field in fields:

      if field.getInputFieldName(0) == "Layer":

           print field.getInputFieldName(0)

     elif field.getInputFieldName(0) == "Text":

           print field.getInputFieldName(0)

     else:

           print field.getInputFieldName(0)

           fieldmappings.removeFieldMap(i)

           i=i+1

Can anyone help me please?

0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

If you only want to preserve a few fields, why don't you use the "Feature Class to Feature class" tool and only include the two fields you want to keep.   Feature Class To Feature Class—Help | ArcGIS for Desktop    This seems simpler than trying to delete a bunch of fields.

You can run a test manually to get the format of the fields you need to keep (if you have to do this multiple times, so you need it in a script).

Here's a sample...

tool, before the delete....will delete all but PtID and the two elev_ fields

my new featureclass....

The "code snippet" from the results tab....

arcpy.FeatureClassToFeatureClass_conversion(in_features="C:/_beartest/Prep.gdb/Pts1_dd", out_path="C:/_beartest/Prep.gdb", out_name="testoutput", where_clause="", field_mapping="""PtID "Ptno" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,PtID,-1,-1;elev_ft "elev_ft" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_ft,-1,-1;elev_m "elev_m" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_m,-1,-1""", config_keyword="")

....since the formatted snippet is off the screen...I'll include it as just the text.  Of course, you do not need the "fieldname=" in each of the arguments....

arcpy.FeatureClassToFeatureClass_conversion(in_features="C:/_beartest/Prep.gdb/Pts1_dd", out_path="C:/_beartest/Prep.gdb", out_name="testoutput", where_clause="", field_mapping="""PtID "Ptno" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,PtID,-1,-1;elev_ft "elev_ft" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_ft,-1,-1;elev_m "elev_m" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_m,-1,-1""", config_keyword="")

Sometimes you just need to sit back and see if there is a simpler way.  Hope this helps.

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

dedent the i=i+1 line, it shouldn't line up with the line above, but with the else otherwise, the i=i+1 doesn't get incremented properly

i = 0
for
    if .....
        do stuff
    elif.....
        do  other
    else:
        done
    i = i+1
0 Kudos
ZdeněkSoldán
Regular Contributor

Thank you but it doesn't solve my problem. I have still the same error message.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ahhh you are getting an error message...

Could you copy and paste it?  I thought it might be a problem with the "i" incrementation.

0 Kudos
ZdeněkSoldán
Regular Contributor

Here is the error message:

Traceback (most recent call last):

  File "<interactive input>", line 8, in <module>

  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\arcobjects.py", line 336, in removeFieldMap

    return convertArcObjectToPythonObject(self._arc_object.RemoveFieldMap(*gp_fixargs(args)))

RuntimeError: FieldMappings: Error in removing a field map from field mappings for RemoveFieldMap

But I don't understand why can I remove a half of fields and only after that the error message appears.

0 Kudos
LukeWebb
Frequent Contributor

1) What line of your code corresponds to:  

File "<interactive input>", line 8, in <module>

2) Where do you populate the 'fields' variable?

3) Field maps could be replaced by an Append step in your code, create the template table with 2 fields within, and just use the append tool with Schema type set to "NO_TEST".

We dont have enough information to diagnose easily sorry!

RebeccaStrauch__GISP
MVP Emeritus

If you only want to preserve a few fields, why don't you use the "Feature Class to Feature class" tool and only include the two fields you want to keep.   Feature Class To Feature Class—Help | ArcGIS for Desktop    This seems simpler than trying to delete a bunch of fields.

You can run a test manually to get the format of the fields you need to keep (if you have to do this multiple times, so you need it in a script).

Here's a sample...

tool, before the delete....will delete all but PtID and the two elev_ fields

my new featureclass....

The "code snippet" from the results tab....

arcpy.FeatureClassToFeatureClass_conversion(in_features="C:/_beartest/Prep.gdb/Pts1_dd", out_path="C:/_beartest/Prep.gdb", out_name="testoutput", where_clause="", field_mapping="""PtID "Ptno" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,PtID,-1,-1;elev_ft "elev_ft" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_ft,-1,-1;elev_m "elev_m" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_m,-1,-1""", config_keyword="")

....since the formatted snippet is off the screen...I'll include it as just the text.  Of course, you do not need the "fieldname=" in each of the arguments....

arcpy.FeatureClassToFeatureClass_conversion(in_features="C:/_beartest/Prep.gdb/Pts1_dd", out_path="C:/_beartest/Prep.gdb", out_name="testoutput", where_clause="", field_mapping="""PtID "Ptno" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,PtID,-1,-1;elev_ft "elev_ft" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_ft,-1,-1;elev_m "elev_m" true true false 2 Short 0 0 ,First,#,C:\_beartest\Prep.gdb\Pts1_dd,elev_m,-1,-1""", config_keyword="")

Sometimes you just need to sit back and see if there is a simpler way.  Hope this helps.