AnsweredAssumed Answered

RemoveFieldMap doesn't work on AddJoin table views?

Question asked by mdtinker_USGS on Aug 31, 2017
Latest reply on Aug 31, 2017 by mbabinski1988

I have AddJoined two tables as a table view. Now I want to remove very many fields. I want to operate on this table in memory before I export it because the table is very large and it takes a very long time to export. So, I am starting by reducing the number of fields.

 

In the code below, I am using tiny little tables for testing. 

 

I understand "for field" loop. But I don't understand why it doesn't remove the fields.

 

I print(field.name) as a sanity check. The joined fields of the table view are all there. Then, I export the addjoin table (TableToTable_conversion) to see if the fields really have been removed. They aren't. The exported table contains all original fields. The problem seems to be in the line: fieldmappings.removeFieldMap.  

 

Question: Does FieldMappings.RemoveFieldMap only work on "real" tables, and not on tableviews? Or perhaps it doesn't work on AddJoins? 

 

Thanks in advance for any suggestion.

 

Best regards, Mike

 

 # Make the VAA table view

mr_vaa_view = 'mr_vaa_table_view'

arcpy.MakeTableView_management(mr_vaa, mr_vaa_view)

 

# Join the VAA table to the flowlines
# Must turn the VAA and Flowlines into a table view.
# Because AddJoin only works on layers and views

 

# Make the VAA table view#
mr_vaa_view = 'mr_vaa_table_view'
arcpy.MakeTableView_management(mr_vaa, mr_vaa_view)

 

# Make the MR Flowlines table view#

mr_fl_lyr = 'mr_flowlines_layer'

arcpy.MakeTableView_management(mr_fl_table,mr_fl_lyr)

 

# AddJoin the VAA table, (mr_vaa_view) to the Flowline table, (mr_fl_table)#

addjoin = arcpy.AddJoin_management(mr_fl_lyr, 'Permanent_Identifier', mr_vaa_view, 'ComIDstr', 'KEEP_COMMON')

 

# Reduce the number of fields to the essentials#

fieldmappings = arcpy.FieldMappings()

fieldmappings.addTable(addjoin)

 

# Name fields I want to keep#

keepers = ["ReachCode", "FlowFlowlineVAACopy_Hydroseq","COMstr"]

 

# Remove fields I don't want#

for field in fieldmappings.fields:

      print(field.name)

      if field.name not in keepers:

         fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))

 

name = 'MR_VAA'

out_folder = 'D:\output'

outputGDB = 'output.gdb'

mr_nhd_vaa = os.path.join(out_folder, outputGDB)

arcpy.TableToTable_conversion(addjoin, mr_nhd_vaa, name)

Outcomes