arcpy.management.DeleteField(in_table, drop_field, {method})
|
名前
|
説明
|
データ タイプ
|
|
in_table
|
削除するフィールドを含むテーブル。 既存の入力テーブルが変更されます。
|
Mosaic Layer; Raster Layer; Table View
|
|
drop_field
[drop_field,...]
|
method パラメーターで指定した、入力テーブルから削除または維持するフィールド。 必須でないフィールドのみを削除できます。
|
Field
|
|
method
(オプション)
|
drop_field パラメーターで指定したフィールドを削除するか維持するかを指定します。
|
String
|
派生した出力
|
名前
|
説明
|
データ タイプ
|
|
out_table
|
更新されたデータセット。
|
Table View; Raster Layer; Mosaic Layer
|
コードのサンプル
DeleteField の例 1 (Python ウィンドウ)
次の Python ウィンドウ スクリプトは、指定したフィールドを削除する DeleteField 関数の使用方法を示しています。
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"])
DeleteField の例 2 (Python ウィンドウ)
次の Python ウィンドウ スクリプトは、指定したフィールドのみを維持する DeleteField 関数の使用方法を示しています。
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")
DeleteField の例 3 (スタンドアロン スクリプト)
次のスタンドアロン スクリプトで、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)
DeleteField の例 4 (スタンドアロン スクリプト)
DeleteField 関数を使用して、フィーチャクラスまたはテーブルから不要なすべてのフィールドを削除します。
# 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)