0

I am preparing a table from a polygon feature class in order to write a CSV file. Therefore I want to remove certain fields from the table, as they are not needed in the final CSV product.

I first tried using this method, which did not remove the 'Shape' field from the list. The other two fields were removed successfully though.

import arcpy

workspace = r"X:\TEST\testDEMABOECK.gdb"
version = u"Test"

arcpy.env.workspace = workspace
arcpy.env.scratchWorkspace = workspace
arcpy.env.overwriteOutput = True

outDEMABOCK = """{}\\Testriver_DEMABOECKcalc_{}""".format(workspace, version)

# Table handling
arcpy.MakeTableView_management(outDEMABOCK, "tempTable")

fields = [field.name for field in arcpy.ListFields("tempTable")]
for index, field in enumerate(fields):
    if field == "Shape" or field == "Shape_Length" or field == "OBJECTID":
        del fields[index]

print fields

##[u'Shape', u'typeID', u'Shape_Area', u'hvAWB', u'hvPCA', u'hvRWB', u'hvTIZ', u'kfAWB', u'kfPCA', u'kfRWB', u'kfTIZ', u'typeName', u'Holzkubatur_m3_km2', u'Holzkubatur_m3']

I then found this post on GSE, which guided me in the right direction. The following solution did remove the 'Shape' field.

import arcpy

workspace = r"X:\TEST\testDEMABOECK.gdb"
version = u"Test"

arcpy.env.workspace = workspace
arcpy.env.scratchWorkspace = workspace
arcpy.env.overwriteOutput = True

outDEMABOCK = """{}\\Testriver_DEMABOECKcalc_{}""".format(workspace, version)

# Table handling
arcpy.MakeTableView_management(outDEMABOCK, "tempTable")

notNeeded = ["Shape", "OBJECTID", "Shape_Length"]
fields = [field.name for field in arcpy.ListFields("tempTable") if field.name not in notNeeded]

print fields

##[u'typeID', u'Shape_Area', u'hvAWB', u'hvPCA', u'hvRWB', u'hvTIZ', u'kfAWB', u'kfPCA', u'kfRWB', u'kfTIZ', u'typeName', u'Holzkubatur_m3_km2', u'Holzkubatur_m3']

Why does the second method work but the first does not?

The answer in above link does not go into detail.

2
  • Have you looked up what del does in Python documentation? It is not an ArcPy function.
    – PolyGeo
    Aug 2, 2017 at 6:53
  • I'm sure that it's not a good idea to remove elements from list in a loop. Your solution doesn't affect the initital list but only filters it and creates new one. Thats why it works. Also try to change the order in your list in the first solution (just sort alphabetically) and take a look at a result. Aug 4, 2017 at 6:38

1 Answer 1

2

The del method removes the python variable, it does not impact the ArcGIS object that the variable references.

=> To get the first method to work, you need to call a method that removes the object: arcpy.DeleteField_management(in_table, drop_field)

The second method manipulates the ArcGIS object itself, that is why it works.

1
  • Try to delete Shape (or ObjectID or Shape_Length) field from a table. Aug 4, 2017 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.