Feld löschen (Datenmanagement)

Lizenzstufe:BasicStandardAdvanced

Zusammenfassung

Mit diesem Werkzeug können Sie ein oder mehrere Felder aus einer Tabelle, einer Feature-Class, einem Feature-Layer oder einem Raster-Dataset löschen.

Verwendung

Syntax

DeleteField_management (in_table, drop_field)
ParameterErläuterungDatentyp
in_table

Die Tabelle, die die zu löschenden Felder enthält. Die vorhandene Eingabetabelle wird geändert.

Mosaic Layer; Raster Catalog Layer; Raster Layer; Table View
drop_field
[drop_field,...]

Die Felder, die aus der Eingabetabelle gelöscht werden sollen. Nur Felder, die nicht erforderlich sind, können gelöscht werden.

Field

Codebeispiel

DeleteField – Beispiel (Python-Fenster)

Im folgenden Skript im Python-Fenster wird veranschaulicht, wie das Werkzeug "DeleteField" im unmittelbaren Modus verwendet wird:

import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.CopyFeatures_management("majorrds.shp", "C:/output/majorrds_copy.shp")
arcpy.DeleteField_management("C:/output/majorrds_copy.shp", 
                             ["STREET_NAM", "LABEL", "CLASS"])
DeleteField – Beispiel 2 (eigenständiges Skript)

Das folgende eigenständige Skript veranschaulicht, wie das Werkzeug "DeleteField" verwendet wird:

# Name: DeleteField_Example2.py
# Description: Delete several fields from a feature class
# Author: ESRI
  
# Import system modules
import arcpy
from arcpy import env
 
# Set environment settings
env.workspace = "C:/data"
 
# Set local variables
inFeatures = "accident.dbf"
outFeatureClass = "C:/output/new_accident.dbf"
dropFields = ["STREET_NAM", "LABEL", "CLASS"]
 
# Execute CopyFeatures to make a new copy of the feature class
#  Use CopyRows if you have a table
arcpy.CopyFeatures_management(inFeatures, outFeatureClass)
 
# Execute DeleteField
arcpy.DeleteField_management(outFeatureClass, dropFields)
DeleteField – Beispiel 3 (eigenständiges Skript)

Verwenden Sie "DeleteField" zum Löschen aller unnötigen Felder aus einer Feature-Class oder Tabelle.

# Name: DeleteFields_clearfields.py
# Description: Delete unrequired fields from a feature class or tble.
 
# Import system modules
import arcpy
from arcpy import env
 
try: 
    # Get user-supplied input and output arguments
    inTable = arcpy.GetParameterAsText(0)
    updatedTable = arcpy.GetParameterAsText(1)
 
    # Describe the input (need to test the dataset and data types)
    desc = arcpy.Describe(updatedTable)
 
    # Make a copy of the input (so we can mantain the original as is)
    if desc.datasetType == "FeatureClass":
        arcpy.CopyFeatures_management(inTable, updatedTable)
    else:
        arcpy.CopyRows_management(inTable, updatedTable)
 
    # Use ListFields to get a list of field objects
    fieldObjList = arcpy.ListFields(updatedTable)
 
    # Create an empty list that will be populated with field names        
    fieldNameList = []
 
    # For each field in the object list, add the field name to the
    #  name list.  If the field is required, exclude it, to prevent errors
    for field in fieldObjList:
        if not field.required:
            fieldNameList.append(field.name)
 
    # dBASE tables require a field other than an OID and Shape.  If this is
    #  the case, retain an extra field (the first one in the original list)
    if desc.dataType in ["ShapeFile", "DbaseTable"]:
        fieldNameList = fieldNameList[1:]
 
    # Execute DeleteField to delete all fields in the field list. 
    arcpy.DeleteField_management(updatedTable, fieldNameList)
          
except Exception as e:
    # If an error occurred, print line number and error message
    import traceback
    import sys
    tb = sys.exc_info()[2]
    print("Line {0}".format(tb.tb_lineno))
    print(e.message)

Umgebung

Verwandte Themen

Lizenzierungsinformationen

ArcGIS for Desktop Basic: Ja
ArcGIS for Desktop Standard: Ja
ArcGIS for Desktop Advanced: Ja
5/9/2014