Import arcpy module import arcpy # Get input feature class and output feature class input_fc = arcpy.GetParameterAsText(0) output_fc = arcpy.GetParameterAsText(1) # Get field list of input feature class fields = arcpy.ListFields(input_fc) # Create a list of field names to keep field_names = ["OID@", "SHAPE@", "SHAPE_LENGTH", "SHAPE_AREA"] # Loop through input feature class fields and add field names to field_names list for field in fields: if not (field.name in field_names): # If the field name is not in the field_names list field_names.append(field.name) # Add the field name to the field_names list # Use Make Feature Layer to create a layer from the input feature class arcpy.MakeFeatureLayer_management(input_fc, "temp_layer") # Use Add Field Delimiters to get the correct syntax for field names delimited_fields = arcpy.AddFieldDelimiters("temp_layer", field_names[0]) for i in range(1, len(field_names)): delimited_fields += "," + arcpy.AddFieldDelimiters("temp_layer", field_names[i]) # Use Copy Features to create the output feature class with only the specified fields arcpy.CopyFeatures_management("temp_layer", output_fc, delimited_fields) # Delete the temporary layer arcpy.Delete_management("temp_layer")运行错误:Traceback (most recent call last): File "D:\多对一空间连接\删除所有字段.py", line 28, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 2429, in CopyFeatures raise e ExecuteError: 执行失败。参数无效。 ERROR 000800: 该值不是 | DEFAULTS | TEXT_UTF16 | MAX_FILE_SIZE_4GB | MAX_FILE_SIZE_256TB | GEOMETRY_OUTOFLINE | BLOB_OUTOFLINE | GEOMETRY_AND_BLOB_OUTOFLINE | TERRAIN_DEFAULTS | MOSAICDATASET_DEFAULTS | MOSAICDATASET_INLINE 的成员。 执行(CopyFeatures)失败。 执行(删除所有字段)失败。请改正代码
时间: 2023-12-14 18:36:26 浏览: 176
这个错误是由于 `delimited_fields` 中使用了无效的参数值所引起的。你需要检查 `CopyFeatures_management` 的第三个参数(`field_mapping`)的值是否正确。
在这里,你可以将 `CopyFeatures_management` 中的第三个参数设置为 `""`,以仅复制指定的字段到输出要素类中。修改你的代码如下:
```python
import arcpy
# Get input feature class and output feature class
input_fc = arcpy.GetParameterAsText(0)
output_fc = arcpy.GetParameterAsText(1)
# Get field list of input feature class
fields = arcpy.ListFields(input_fc)
# Create a list of field names to keep
field_names = ["OID@", "SHAPE@", "SHAPE_LENGTH", "SHAPE_AREA"]
# Loop through input feature class fields and add field names to field_names list
for field in fields:
if not (field.name in field_names): # If the field name is not in the field_names list
field_names.append(field.name) # Add the field name to the field_names list
# Use Make Feature Layer to create a layer from the input feature class
arcpy.MakeFeatureLayer_management(input_fc, "temp_layer")
# Use Add Field Delimiters to get the correct syntax for field names
delimited_fields = arcpy.AddFieldDelimiters("temp_layer", field_names[0])
for i in range(1, len(field_names)):
delimited_fields += "," + arcpy.AddFieldDelimiters("temp_layer", field_names[i])
# Use Copy Features to create the output feature class with only the specified fields
arcpy.CopyFeatures_management("temp_layer", output_fc, delimited_fields, "")
# Delete the temporary layer
arcpy.Delete_management("temp_layer")
```
这样,你就可以成功地复制指定的字段到输出要素类中了。
阅读全文