Resumen
Elimina uno o varios campos de una tabla, una clase de entidad, una capa de entidad o un dataset ráster.
Uso
Los campos no se pueden eliminar de formatos de datos de sólo lectura o no nativos de ArcGIS, como los datasets de CAD y VPF.
Sintaxis
DeleteField(in_table, drop_field)
Parámetro | Explicación | Tipo de datos |
in_table | La tabla que contiene los campos que se eliminarán. Se modificará la tabla de entrada existente. | Mosaic Layer; Raster Layer; Table View |
drop_field [drop_field,...] | Los campos que se eliminarán de la tabla de entrada. Sólo se pueden eliminar los campos no obligatorios. | Field |
Salida derivada
Nombre | Explicación | Tipo de datos |
out_table | El dataset actualizado. | Vista de tabla; Capa ráster; Capa de mosaico |
Muestra de código
El siguiente scriptde la ventana de Python muestra cómo utilizar la herramienta DeleteField en el modo inmediato:
import arcpy
arcpy.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"])
El siguiente script independiente muestra cómo utilizar la herramienta DeleteField:
# Name: DeleteField_Example2.py
# Description: Delete several fields from a feature class
# Import system modules
import arcpy
# Set environment settings
arcpy.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)
Utilice DeleteField para eliminar todos los campos que no sean necesarios de una tabla o clase de entidad.
# Name: DeleteFields_clearfields.py
# Description: Delete unrequired fields from a feature class or table.
# Import system modules
import arcpy
# 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)
Entornos
Información de licenciamiento
- Basic: Sí
- Standard: Sí
- Advanced: Sí