该Python脚本是执行此操作的一种“胶带”方法。它限制为20个字段,但是可以轻松添加到该字段中,以使其能够运行于数据中的许多字段。
该脚本旨在作为脚本工具运行,因此您需要在ArcCatalog中设置参数。
参数应如下:
- InputFC-要素类
- 输出位置-工作区
- 输出名称-字符串
- 场1-场
- 场2-场
- 一直到Field20
码:
import arcpy, os, sys
from arcpy import env
arcpy.env.overwriteOutput = True
inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outName = arcpy.GetParameterAsText(2)
field1 = arcpy.GetParameterAsText(3)
field2 = arcpy.GetParameterAsText(4)
field3 = arcpy.GetParameterAsText(5)
field4 = arcpy.GetParameterAsText(6)
field5 = arcpy.GetParameterAsText(7)
field6 = arcpy.GetParameterAsText(8)
field7 = arcpy.GetParameterAsText(9)
field8 = arcpy.GetParameterAsText(10)
field9 = arcpy.GetParameterAsText(11)
field10 = arcpy.GetParameterAsText(12)
field11 = arcpy.GetParameterAsText(13)
field12 = arcpy.GetParameterAsText(14)
field13 = arcpy.GetParameterAsText(15)
field14 = arcpy.GetParameterAsText(16)
field15 = arcpy.GetParameterAsText(17)
field16 = arcpy.GetParameterAsText(18)
field17 = arcpy.GetParameterAsText(19)
field18 = arcpy.GetParameterAsText(20)
field19 = arcpy.GetParameterAsText(21)
field20 = arcpy.GetParameterAsText(22)
fieldList = ["SHAPE@"]
arcpy.AddMessage(" ")
arcpy.AddMessage("Appending field choices to new List ")
arcpy.AddMessage(" ")
if (field1 != ""):
fieldList.append(field1)
if (field2 != ""):
fieldList.append(field2)
if (field3 != ""):
fieldList.append(field3)
if (field4 != ""):
fieldList.append(field4)
if (field5 != ""):
fieldList.append(field5)
if (field6 != ""):
fieldList.append(field6)
if (field7 != ""):
fieldList.append(field7)
if (field8 != ""):
fieldList.append(field8)
if (field9 != ""):
fieldList.append(field9)
if (field10 != ""):
fieldList.append(field10)
if (field11 != ""):
fieldList.append(field1)
if (field12 != ""):
fieldList.append(field12)
if (field13 != ""):
fieldList.append(field13)
if (field14 != ""):
fieldList.append(field14)
if (field15 != ""):
fieldList.append(field15)
if (field16 != ""):
fieldList.append(field16)
if (field17 != ""):
fieldList.append(field17)
if (field18 != ""):
fieldList.append(field18)
if (field19 != ""):
fieldList.append(field19)
if (field20 != ""):
fieldList.append(field20)
arcpy.AddMessage(" ")
#arcpy.AddMessage(fieldList)
oldFieldList = arcpy.ListFields(inFC)
fieldTypes = []
numOfFields = len(fieldList)
fieldIndex = 1
reorderedFields = []
for fld in fieldList:
for f in oldFieldList:
if f.name == fld:
arcpy.AddMessage(f.name)
reorderedFields.append(f)
break
arcpy.AddMessage(" ")
arcpy.AddMessage(reorderedFields)
desc = arcpy.Describe(inFC)
geoType = desc.shapeType.upper()
spatRef = arcpy.Describe(inFC).spatialReference
arcpy.CreateFeatureclass_management(outLoc, outName, geoType, "", "", "", spatRef)
newFeat = os.path.join(outLoc, outName)
for flds in reorderedFields:
if (flds.type == "String"):
fLength = flds.length
arcpy.AddField_management(newFeat, flds.name, flds.type, "", "", str(fLength))
else:
arcpy.AddField_management(newFeat, flds.name, flds.type)
arcpy.AddMessage(" ")
arcpy.AddMessage(fieldList)
arcpy.AddMessage(" ")
arcpy.AddMessage("Features will be copied with new Schema...")
count = 0
with arcpy.da.SearchCursor(inFC, fieldList) as cursor:
for row in cursor:
insertCursor = arcpy.da.InsertCursor(newFeat, (fieldList))
if (numOfFields == 21):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]) + ', ' + str(row[20]))
count += 1
elif (numOfFields == 20):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]))
count += 1
elif (numOfFields == 19):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]))
count += 1
elif (numOfFields == 18):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]))
count += 1
elif (numOfFields == 17):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]))
count += 1
elif (numOfFields == 16):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]))
count += 1
elif (numOfFields == 15):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]))
count += 1
elif (numOfFields == 14):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]))
count += 1
elif (numOfFields == 13):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]))
count += 1
elif (numOfFields == 12):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]))
count += 1
elif (numOfFields == 11):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]))
count += 1
elif (numOfFields == 10):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]))
count += 1
elif (numOfFields == 9):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]))
count += 1
elif (numOfFields == 8):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]))
count += 1
elif (numOfFields == 7):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]))
count += 1
elif (numOfFields == 6):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]))
count += 1
elif (numOfFields == 5):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]))
count += 1
elif (numOfFields == 4):
insertCursor.insertRow((row[0], row[1], row[2], row[3]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]))
count += 1
elif (numOfFields == 3):
insertCursor.insertRow((row[0], row[1], row[2]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]))
count += 1
elif (numOfFields == 2):
insertCursor.insertRow((row[0], row[1]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]))
count += 1
我意识到这不是最漂亮或最有效的方法,但实际上效果很好,只花了半个小时就写了。当我需要重新排序字段时,我会一直使用它,并且不需要Diagrammer或Microsoft Access。当然,最大字段数可能是一个问题,但同样可以在脚本中自定义。