我需要做什么:
- 重命名表/功能类的字段名称
- 将所有值复制到新字段
到目前为止,我已经按照@ artwork21的建议完成了以下代码:
import sys
import traceback
import arcpy
from arcpy import env
## ARGUMENTS
# argv[1] = input table/feature class path
# argv[2] = input old field name
# argv[3] = input new field name
path = sys.argv[1]
oldFieldName = sys.argv[2]
newFieldName = sys.argv[3]
env.overwriteOutput = True
fields = arcpy.ListFields(path)
for field in fields:
if field.aliasName == oldFieldName:
if not oldFieldName == newFieldName:
fieldType = field.type
# Add new field
arcpy.AddField_management(path, newFieldName, fieldType)
#Calculates the new field based on old field values
arcpy.CalculateField_management(path, newFieldName, "!"+oldFieldName+"!", "PYTHON", "")
# Delete the old field (if necessary)
arcpy.DeleteField_management(path, oldFieldName)
如何映射field.type
到AddField_management
方法的字段类型?而当字段位于中间位置时,则将该字段从中间删除并添加到最后一个字段。看起来好像没有重命名字段名称。
有没有更好的解决方案可以帮助我完成这些任务?
old_field
变量名还是旧字段的实际名称?如果它是一个变量名,则需要使用字符串格式设置或串联操作来将变量的值括在方括号(VB解析器)或感叹号(Python解析器)中。