-1

I want to export a table but would like to remove one of the fields before doing so using arcpy. I'm trying to use field mappings but am missing something:

fields = arcpy.ListFields("some_table")

for field in infields:
    print field.name

OBJECTID
APN_GROUND
APN_AIR
DATE_CREATE
DATE_UPDATE
EDITOR
COMMENT

fmap = arcpy.FieldMappings()
fmap.addTable("some_table")

fmap1 = fmap.removeFieldMap(fmap.findFieldMapIndex("Editor"))
arcpy.TableToTable_conversion("some_table","some_path","new_table","",fmap1)

The tool runs but the field is present in the output.

  • 2
    EDITOR != Editor – FelixIP Nov 21 '18 at 3:10
  • Where is "some_table" stored? – PolyGeo Nov 21 '18 at 4:58
  • @FelixIP Yes. I did correct the capitalization. Same result, however. – geoJshaun Nov 21 '18 at 19:18
  • @PolyGeo Some table is stored on an sde. – geoJshaun Nov 21 '18 at 19:21
  • Does your code work when "some_table" is copied into a file geodatabase? I think trying to reproduce ArcSDE issues in a File Geodatabase should always be an early step in troubleshooting them. – PolyGeo Nov 21 '18 at 21:58
2

You can create a function to hide fields (this can also be adapted to set field ratio etc. which is what I use it for):

import arcpy

fishnet = r'C:\Default.gdb\Fishnet_sample'
print([f.name for f in arcpy.ListFields(fishnet)])

def hide_fields(in_fc, fields_to_hide_list):
    fc_desc = arcpy.Describe(in_fc)
    fl = arcpy.MakeFeatureLayer_management(in_fc,fc_desc.baseName)
    src_field_info = arcpy.Describe(fl).fieldInfo
    for field in [f.name for f in arcpy.ListFields(in_fc)]:
        field_idx = src_field_info.findfieldbyname(field)
        if field in fields_to_hide_list:
            src_field_info.setVisible(field_idx,'HIDDEN')
        else:
            src_field_info.setVisible(field_idx,'VISIBLE')
    newfl = arcpy.MakeFeatureLayer_management(in_fc,in_fc+'_fl',field_info=src_field_info)
    return newfl

print([f.name for f in arcpy.ListFields(hide_fields(in_fc=fishnet, fields_to_hide_list=['Urk','RUTA']))])

#arcpy.TableToTable_conversion(in_rows=hide_fields(in_fc=fishnet, fields_to_hide_list=['Urk','RUTA']),...

Print statements output:

['OBJECTID_12', 'Shape', 'OBJECTID_1', 'OBJECTID', 'RUTA', 'RUTANUM', 'Shape_Leng', 'Ruta_10km', 'RutaNum_10', 'Shape_Length', 'Shape_Area', 'Seq', 'Urk']
['OBJECTID_12', 'Shape', 'OBJECTID_1', 'OBJECTID', '', 'RUTANUM', 'Shape_Leng', 'Ruta_10km', 'RutaNum_10', 'Shape_Length', 'Shape_Area', 'Seq', '']
1

I'm not sure why removing the field doesn't remove it from your output. I've found the following to work:

  1. Creating a new field mappings object
  2. Iterate through your original field mappings object adding each field to the new field mappings object. Skip adding the field you wish to not be in the new table.

Sample:

 infmaps = arcpy.FieldMappings()
 infmaps.addTable ("sometable")
 outfmaps = arcpy.FieldMappings ()
 for fld in infmaps.fieldMappings:
     if fld.outputField.name == "Editor": continue
     outfmaps.addFieldMap (fld)

 arcpy.TableToTable_conversion("some_table","some_path","new_table","",outfmaps)
  • I used your code but field is still there – geoJshaun Nov 21 '18 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.