# Import arcpy moduleimport arcpy# Define the input feature class or tablefc = arcpy.GetParameterAsText(0)# Get a list of all fields in the input feature class or tablefields = arcpy.ListFields(fc)# Loop through each field and delete itfor field in fields: arcpy.DeleteField_management(fc, field.name)# Print a message to indicate that the fields have been deletedarcpy.AddMessage("All fields have been deleted from " + fc)运行出错:Traceback (most recent call last): File "D:\多对一空间连接\删除所有字段.py", line 12, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3479, in DeleteField raise e ExecuteError: 执行失败。参数无效。 ERROR 001334: 无法删除必需字段 OBJECTID 执行(DeleteField)失败。请改正代码
时间: 2023-12-11 16:02:39 浏览: 27
这个错误是因为你试图删除必需字段 "OBJECTID",这是每个要素类的默认字段,无法删除。如果你想删除除了 "OBJECTID" 以外的所有字段,可以在删除之前添加一个条件来排除这个字段。修改你的代码如下:
```python
# Import arcpy module
import arcpy
# Define the input feature class or table
fc = arcpy.GetParameterAsText(0)
# Get a list of all fields in the input feature class or table
fields = arcpy.ListFields(fc)
# Loop through each field and delete it (except for OBJECTID)
for field in fields:
if field.name != "OBJECTID":
arcpy.DeleteField_management(fc, field.name)
# Print a message to indicate that the fields have been deleted
arcpy.AddMessage("All fields (except for OBJECTID) have been deleted from " + fc)
```
这个修改后的代码应该可以正常运行,并且不会删除必需字段 "OBJECTID"。
相关问题
写一个Python2.7的arcgis10.22工具箱脚本:删除数据的所有字段
以下是一个Python 2.7的ArcGIS 10.2.2工具箱脚本,可以用于删除一个要素类或表的所有字段:
```python
# Import arcpy module
import arcpy
# Define the input feature class or table
fc = arcpy.GetParameterAsText(0)
# Get a list of all fields in the input feature class or table
fields = arcpy.ListFields(fc)
# Loop through each field and delete it
for field in fields:
arcpy.DeleteField_management(fc, field.name)
# Print a message to indicate that the fields have been deleted
arcpy.AddMessage("All fields have been deleted from " + fc)
```
注意,这个脚本需要在ArcGIS Desktop中运行,并且需要在工具箱中创建一个新的脚本工具,并将此脚本作为该工具的主体。在工具箱中创建新工具时,确保将一个参数类型设置为“Feature Class”或“Table”,并将该参数映射到脚本中的fc变量上。
写一个arcgis工具箱脚本:用来删除要素图层的所有自定义字段
下面是一个简单的 ArcGIS 工具箱脚本示例,可以用来删除指定要素图层的所有自定义字段:
```python
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "删除自定义字段"
self.alias = "Delete Custom Fields"
# Define tool parameters
self.params = [
arcpy.Parameter(
displayName="输入要素图层",
name="input_layer",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input"),
]
def getParameterInfo(self):
return self.params
def execute(self, parameters, messages):
# Get the input layer parameter
input_layer = parameters[0].valueAsText
# Get a list of all fields in the input layer
field_list = arcpy.ListFields(input_layer)
# Loop through the fields and delete any that are not system fields
for field in field_list:
if not field.required:
arcpy.DeleteField_management(input_layer, field.name)
# Print a message when the script has finished running
messages.addMessage("已删除所有自定义字段。")
```
在 ArcGIS 中创建一个新的工具箱,然后将上述代码保存为 Python 脚本,并将其添加到工具箱中。在工具箱中,将参数设置为要删除自定义字段的要素图层,然后运行脚本即可。