arcpy.management.DeleteField(in_table, drop_field, {method})
|
Nombre
|
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 deben eliminarse o conservarse de la tabla de entrada, tal como se especifica en el parámetro method. Solo se pueden eliminar los campos no obligatorios.
|
Field
|
|
method
(Opcional)
|
Especifica si los campos especificados por el parámetro drop_field se eliminarán o se conservarán.
DELETE_FIELDS—Se eliminarán los campos especificados por el parámetro drop_field. Esta es la opción predeterminada.
KEEP_FIELDS—Los campos especificados por el parámetro drop_field se conservarán; todos los demás campos se eliminarán.
|
String
|
Salida derivada
|
Nombre
|
Explicación
|
Tipo de datos
|
|
out_table
|
El dataset actualizado.
|
Table View; Raster Layer; Mosaic Layer
|
Muestra de código
Ejemplo 1 de "DeleteField" (ventana de Python)
El siguiente script de la ventana de Python muestra cómo utilizar la función DeleteField para eliminar los campos especificados.
import arcpy
arcpy.env.workspace = "C:/data"
arcpy.management.CopyFeatures("majorrds.shp", "C:/output/majorrds_copy.shp")
arcpy.management.DeleteField("C:/output/majorrds_copy.shp",
["STREET_NAM", "LABEL", "CLASS"])
Ejemplo 2 de "DeleteField" (ventana de Python)
El siguiente script de la ventana de Python muestra cómo utilizar la función DeleteField para conservar solo los campos especificados.
import arcpy
arcpy.env.workspace = "C:/data"
arcpy.management.CopyFeatures("majorrds.shp", "C:/output/majorrds_copy.shp")
arcpy.management.DeleteField("C:/output/majorrds_copy.shp",
["STREET_ALIAS", "DISTRICT_ID"], "KEEP_FIELDS")
Ejemplo 3 de "DeleteField" (script independiente)
El siguiente script independiente muestra cómo utilizar la función DeleteField.
# Name: DeleteField_Example3.py
# Description: Keep several fields from a feature class and delete all the rest of the fields
# 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"
fields = ["STREET_NAM", "LABEL", "CLASS"]
method = "KEEP_FIELDS"
# Run CopyFeatures to make a new copy of the feature class
# Use CopyRows if you have a table
arcpy.management.CopyFeatures(inFeatures, outFeatureClass)
# Run DeleteField
arcpy.management.DeleteField(outFeatureClass, fields, method)
Ejemplo 4 de "DeleteField" (script independiente)
Utilice la función DeleteField para eliminar todos los campos que no sean necesarios de una tabla o clase de entidad.
# Description: Delete unnecessary 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(inTable)
# Make a copy of the input (so you can maintain the original as is)
if desc.datasetType == "FeatureClass":
arcpy.management.CopyFeatures(inTable, updatedTable)
else:
arcpy.management.CopyRows(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. Exclude required fields 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:]
# Run DeleteField to delete all fields in the field list.
arcpy.management.DeleteField(updatedTable, fieldNameList)