# 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 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 deletedarcpy.AddMessage("All fields (except for OBJECTID) have been deleted from " + fc)运行错误:Traceback (most recent call last): File "D:\多对一空间连接\删除所有字段.py", line 13, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3479, in DeleteField raise e ExecuteError: 执行失败。参数无效。 ERROR 001334: 无法删除必需字段 SHAPE 执行(DeleteField)失败。 执行(删除所有字段)失败。
时间: 2023-12-11 22:02:48 浏览: 124
使用arcpy获取简单的矢量数据信息.zip_arcPy 查询_arcpy 数据读入_arcpy 读取数据_arcpy矢量拼接
这个错误是因为你尝试删除了必须的字段 SHAPE。在 ArcGIS 中,如果要删除要素类或表的所有字段,必须首先删除 SHAPE 字段,然后删除其他字段。您可以修改代码以先删除 SHAPE 字段,再删除其他字段,如下所示:
```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 and SHAPE)
for field in fields:
if field.name != "OBJECTID" and field.name != "SHAPE":
arcpy.DeleteField_management(fc, field.name)
# Print a message to indicate that the fields have been deleted
arcpy.AddMessage("All non-required fields have been deleted from " + fc)
```
在这个修改后的代码中,我们添加了一个条件来检查字段名是否为 OBJECTID 或 SHAPE,如果不是,则删除该字段。现在,您应该能够成功地删除要素类或表的所有非必需字段。
阅读全文