arcpy 批量添加字段
今天和一个妹纸聊天她说她现在在做农经权项目,不是科班出身的她对arcgis充满仇恨,她提到做完一个小组又要重新建shp和属性字段,而且属性字段超多记不住啊!我说重覆劳动事情就要arcpy帮我们做把。她说那就交给你。。。。。。。。。。。。
一下懵懂了,于是就拼命的找资料终于找到一份源码,像我这种懒人直接复制——粘贴,在执行时不报错就是不出结果。又懵懂了!于是只能一遍又一遍看源码,不懂查看帮助文档。经过两小时折腾才调试成功!
import arcpy
import os
# 引入当前地图文档
mymxd =arcpy.mapping.MapDocument("current")
ary =arcpy.mapping.ListLayers(mymxd)
fieldname=["BSM","DKMC","DKXH","ZDZL","DKDZ","DKXZ","DKNZ","DKBZ","SYQXZ","CBJYQZBM","SCMJM","SCMJ","SFJBNT","HTMJ","ELYBMJM","ELYBMJ","TDLYLX","TDYT","DLDJ","CBFMC","CBFBM","DKLB"]
aliasname=["标识码","地块名称","地块序号","宗地坐落","地块东至","地块西至","地块南至","地块北至","所有权性质","承包经营权编码","实测面积(亩)","实测面积","是否基本农田","合同面积","二轮延包面积(亩)","二轮延包面积","土地利用类型","土地用途","地力等级","承包方名称","承包方编码","地块类别"]
def AddNewField2(in_table,fieldname):
zidian ={}
i= 0
while i <=21:
zidian[fieldname[i]]=aliasname[i]
i=i+1
for fn in fieldname:
arcpy.AddField_management(in_table,fn,"TEXT")
for lyr in ary:
in_table= lyr.dataSource
if in_table.find(".shp") > 0:
AddNewField2(in_table,fieldname)
else:
next
arcpy.DeleteField_management(in_table,"Id")
del mymxd
arcpy.RefreshActiveView()
转载自:https://blog.csdn.net/wywywywywywy123456/article/details/52089236