Select to view content in your preferred language

Delete dbf fields

1463
3
04-01-2013 02:48 PM
StefaniGermanotta
Occasional Contributor
I have a dbf table with four fields: OID,TYPE,FREQUENCY,COUNT.

I only want to keep TYPE. I got this script off this forum and modified it to fit my data but it is not working. Is that because it is a dbf file? It returns the correct fields for the "print field" command. Any other suggesestions?

test_dbf = "C:\\TempData\\testdata.dbf"

data_description = arcpy.Describe(test_dbf)
fields = [f.name for f in data_description.fields if f.type not in ["Geometry", "Raster", "Blob"]]

#fields = [f.name for f in arcpy.ListFields(test_dbf)  <> 'Geometry'] # This didn't work either
print fields

for i,f in enumerate(fields):
    if f == 'OID' or f == 'FREQUENCY' or f == 'COUNT':
        del fields


Thank you in advance
Tags (2)
0 Kudos
3 Replies
curtvprice
MVP Alum
I have a dbf table with four fields: OID,TYPE,FREQUENCY,COUNT.

I only want to keep TYPE.


This should do what you want (except you can't delete the object ID field):

test_dbf = "C:\\TempData\\testdata.dbf"
# one of more fields to keep in a list
keepers = ["TYPE"]
# list all field delete candidates
fields  = [f.name for f in arcpy.Describe(test_dbf).Fields \
    if f.type not in ["Geometry, "Raster", "BLOB", "OID"]]
# remove  TYPE field from delete list
fields  = [f for f in fields if f.upper() not in keepers]
arcpy.DeleteFields_management(test_dbf, fields)
0 Kudos
StefaniGermanotta
Occasional Contributor
This should do what you want (except you can't delete the object ID field):

test_dbf = "C:\\TempData\\testdata.dbf"
# one of more fields to keep in a list
keepers = ["TYPE"]
# list all field delete candidates
fields  = [f.name for f in arcpy.Describe(test_dbf).Fields \
    if f.type not in ["Geometry, "Raster", "BLOB", "OID"]]
# remove  TYPE field from delete list
fields  = [f for f in fields if f.upper() not in keepers]
arcpy.DeleteFields_management(test_dbf, fields)


Pesky OID. I don't want this field either. I will eventually be exporting to csv file but it doesn't seem any easier to get rid of it as a csv either.
0 Kudos
RDHarles
Regular Contributor
In a bind, I've used this program (CSVed), can do just about anything to a csv file...

http://csved.sjfrancke.nl/#csved

0 Kudos