I have a feature class that I would like to export the attributes to Excel, while excluding certain fields. I know I could make a copy of the feature class and delete fields, but I would like to use the field info object to exclude the unnecessary fields while creating a table view. The following code still exports all the fields, can you tell me what I am missing?
After creating the table view I have tried, using copy rows instead of export to excel and got the same results. I also tried fieldinfo.removeField(index) for the fields I want to exclude but that returned an error (RuntimeError: ERROR 999999: Error executing function). Thanks for your help.
#---- Make Table View that only contains the needed fields and copy to Excel table # Get the fields from the input feature class fields= arcpy.ListFields(in_fc) # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Iterate through the fields and set them to fieldinfo for field in fields: # If field name is in list of fields to keep, if field.name in out_fields: # Add to fieldinfo object fieldinfo.addField(field.name, field.name, "VISIBLE", "") # Make the table view with only desired fields arcpy.MakeTableView_management(in_fc, "table_view", field_info = fieldinfo) # Copy to Excel table arcpy.TableToExcel_conversion("table_view", out_table)