Remove All Field Mapping for arcpy.FeatureClassToFeatureClass_conversion

4736
5
Jump to solution
04-06-2015 01:05 PM
RichardKammer
New Contributor II

Can anyone please tell me why this is not working? When I use the FeatureClassToFeatureClass_conversion geoprocessing tool, I do not want any of the existing fields to be in the output file.

fldmap = arcpy.FieldMappings()
remfldmap = fldmap.removeAll()
arcpy.FeatureClassToFeatureClass_conversion(lyr,GDB,fc,"",remfldmap,"")

					
				
			
			
				
			
			
				
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I'm not sure what the best way to do what you want to do is, but here is my understanding of what's going wrong:

>>> fldmap = arcpy.FieldMappings()
>>> print type(fldmap)
<class 'arcpy.arcobjects.arcobjects.FieldMappings'>
>>> remfldmap = fldmap.removeAll()
>>> print type(remfldmap)
<type 'NoneType'>

You're trying to use 'remfldmap' as an empty field map object, when it is actually a completely empty object (i.e. not a field map).

edit: you can generate unattributed geometries using code like below:

>>> polygons = []
... with arcpy.da.SearchCursor("YOUR_FEATURE_CLASS","SHAPE@") as cursor:
...     for row in cursor:
...         polygons.append(row[0])
... arcpy.CopyFeatures_management(polygons,r"OUTPUT_FEATURE_CLASS")

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

I'm not sure what the best way to do what you want to do is, but here is my understanding of what's going wrong:

>>> fldmap = arcpy.FieldMappings()
>>> print type(fldmap)
<class 'arcpy.arcobjects.arcobjects.FieldMappings'>
>>> remfldmap = fldmap.removeAll()
>>> print type(remfldmap)
<type 'NoneType'>

You're trying to use 'remfldmap' as an empty field map object, when it is actually a completely empty object (i.e. not a field map).

edit: you can generate unattributed geometries using code like below:

>>> polygons = []
... with arcpy.da.SearchCursor("YOUR_FEATURE_CLASS","SHAPE@") as cursor:
...     for row in cursor:
...         polygons.append(row[0])
... arcpy.CopyFeatures_management(polygons,r"OUTPUT_FEATURE_CLASS")
DanPatterson_Retired
MVP Emeritus

don't have anything to try it on, but my guess it is one of those methods that you can't assign but is done in place ... did you just try

import arcpy  
input_shp = 'c:/temp/Phish_Nyet3.shp'  
fm = arcpy.FieldMap()  
  
flds = arcpy.ListFields(input_shp)  
for field in flds:  
if not field.required:  
  fm.addInputField(input_shp,field.name)  
  print field.name  
fm.removeAll()  
fldMapping = arcpy.FieldMappings()  
fldMapping.addFieldMap(fm)  

arcpy.FeatureClassToFeatureClass_conversion(input_shp,'c:\\temp\\',
                                            'All_gone.shp',
                                            field_mapping=fldMapping)  

sorry don't have arc running now to test it

EDIT the original was done in the interactive window.  I removed and pasted if from a script window just in casei the indenentation was throwning people

0 Kudos
curtvprice
MVP Esteemed Contributor

My suggestion: create an empty feature class with the Create Feature Class tool, and dump your data onto it with Append_management with the "NO_TEST" option.

0 Kudos
RichardKammer
New Contributor II

Dan,

I get the following error when I run you example.

It errors on the fm.addInputField(input_shp,field.name) line.

RuntimeError: FieldMap: Error in adding input field to field map

0 Kudos
DanPatterson_Retired
MVP Emeritus

I edited the original post....have a look

the original was done in the interactive window.  I removed and pasted if from a script window just in casei the indenentation was throwning people

0 Kudos