shapefile添加字段 设置文件名为字段内容

转眼间,这一年又结束了,再记录一点知识吧

同事说他有好多shapefile,想给每个shapefile添加一字段,并设置该字段的内容为shapefile文件名,想着用arcpy实现,于是有了下面的代码

import os
import arcpy
arcpy.env.workspace = r"F:\workspace\2017\other"

fcs = arcpy.ListFeatureClasses()  

for fc in fcs:
    arcpy.AddField_management(fc,"Test2","TEXT","","","100","","NULLABLE","NON_REQUIRED")
  
for fc in fcs:
   cursor = arcpy.UpdateCursor(fc)
   feminus = str(fc)[::-1]
   fea = str(feminus)[4:]
   feature = str(fea)[::-1]
   for row in cursor:
       row.setValue("Test2", feature) 
       cursor.updateRow(row)

由于是测试代码,添加的字段名为Test2,文本型,长度为100。

posted on 2017-12-21 14:55  Lzhm216  阅读(1149)  评论(0编辑  收藏  举报

导航