How to Delete Fields from File Geodatabase Table using ArcPy

3360
8
Jump to solution
01-23-2017 12:43 PM
JohnPapageorgiou
New Contributor III

Hi,

I'm wondering if someone can help. I'm trying to write a python script that will:
1. Create a buffer
2. Use the buffer to query a feature class
3. Export a file geodatabase table of only the selected features
4. Delete unwanted fields
5. Export the cleaned up table to excel

Steps 1-3 work fine. I can even get that table to export to excel. I am having difficulty with step 4. I've tried various methods to do this but to no avail. Attached is a copy of the script. The Workspace, feature to buffer and proj_prefix are all input parameters not included in the snippet.

Thanks for any help in advance.

John

0 Kudos
1 Solution

Accepted Solutions
FC_Basson
MVP Regular Contributor

I think your problem lies in line 161 where you are defining the delete table - you are using the same syntax as for the TableToTable conversion tool, which is incorrect.  I believe it should be

countyTable = "gis_data.gdb/" + proj_prefix + "_ca_counties"

View solution in original post

8 Replies
RebeccaStrauch__GISP
MVP Emeritus

Hint: use https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...‌ to make it easier for people to read you code and comment, versus having to open and view a snapshot.

If I understand correctly, you do not want to include some fields in the outputted Excel file.  Try including the Fields you want to include using the Field Mapping.  It is sometimes easier to run it manually first, then copy the python snippet from the results tab to see the format it wants.

Table To Table (Conversion)

TableToTable_conversion (in_rows, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})
ParameterExplanation

A couple other possibilities.....if your are trying to delete required fields or the file has a lock

JohnPapageorgiou
New Contributor III

Hi Rebecca,

I'm looking into the field_mapping option right now.  I'll keep your hint in mind for the next time I post a question.

thanks,

John

0 Kudos
DanPatterson_Retired
MVP Emeritus

Delete Field—Help | ArcGIS Desktop 

Are you sure the fields exist in the file you are creating? Case is important as well. you may need to add a check to see if the field exists prior to deleting it Field—Help | ArcGIS Desktop 

JohnPapageorgiou
New Contributor III

Hi Dan,

The fields are making it to the output table I create.  Is that what you mean?  Can you elaborate on "add a check", do you mean create a script that list the fields?

thanks,

John

0 Kudos
DanPatterson_Retired
MVP Emeritus

You can get the field object and their names readily

field_names = [f.name for f in arcpy.ListFields(featureclass)]

Once you have those, you should check that the field names that you want to delete are actually there

How about an example to demonstrate what I mean

>>> del_flds = ["ArEa", "AREA", "Area", "NotArea"]
>>> fld_list = ["Area","length", "etc"]
>>> to_delete = [i for i in del_flds if i in fld_list]
>>> to_delete
['Area']

Then you can use the DeleteField Delete Field—Help | ArcGIS Desktop 

FC_Basson
MVP Regular Contributor

I think your problem lies in line 161 where you are defining the delete table - you are using the same syntax as for the TableToTable conversion tool, which is incorrect.  I believe it should be

countyTable = "gis_data.gdb/" + proj_prefix + "_ca_counties"
JohnPapageorgiou
New Contributor III

Thanks everyone for your contributions!  FC Basson your suggestion solved the problem.  Thank you so much!

0 Kudos
DougHaller1
New Contributor III

Hi All,

I am working with a similar challenge. 

Is there a way to keep the fields one wants rather than remove unwanted fields? If so, could one then create a table that is a subset of the original and save it to a new folder with a new name?

I will read about Field Mapping.  

Also, would it be better to manage the fields when they are shapefiles or convert the .shp to a .xls first?

Thanks.

Doug

0 Kudos