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)
Solved! Go to Solution.
Hi Michael,
For your Table to Table function it looks like you've omitted the field mappings object from the parameters. Try something like:
arcpy.TableToTable_conversion(addjoin, mr_nhd_vaa, name, "#", fieldmappings)
Also it helps to post your code using the Syntax highlighter (under the "More" menu in the formatting widgets) to make it a bit more readable.
Micah
Hi Michael,
For your Table to Table function it looks like you've omitted the field mappings object from the parameters. Try something like:
arcpy.TableToTable_conversion(addjoin, mr_nhd_vaa, name, "#", fieldmappings)
Also it helps to post your code using the Syntax highlighter (under the "More" menu in the formatting widgets) to make it a bit more readable.
Micah
Micah,
Thank you, that did the trick! And thanks for the heads up on the Syntax highlighter, I will use it next time.
Finally, what does "#" mean? Does this mean to ignore that parameter?
Yes sir, it does! Nice use of the field mapping object, by the way. I may be able to make use of that technique in some of my own scripts.
Cheers,
Micah