arcpy 给shp文件添加字段、赋值

1、添加字段

import arcpy  #加入字段
import os
from arcpy import env
pathfile = r"C:\Users\xxx\Documents\ArcGIS\Default1.gdb"
#env.workspace = r"C:\Users\xiezy\Documents\ArcGIS\Default1.gdb"
for fiel in os.listdir(pathfile):
     if fiel[-3:] == "shp":
           arcpy.AddField_management(fiel,"landId","TEXT",field_length=12)
        arcpy.AddField_management(fiel,"buildingId","TEXT",field_length=12)
	        arcpy.AddField_management(fiel,"floorId","TEXT",field_length=12)
print("finish!")

2、赋值

import arcpy   #给字段赋值
import os
from arcpy import env
pathfile = r"C:\Users\xxx\Documents\ArcGIS\Default1.gdb"
for i in os.listdir(pathfile):
    f i[-3:] == "shp":
    cursor = arcpy.UpdateCursor(os.path.join(pathfile,i))
    id = [i[0:4],i[5:8],i[9:12]]
    for row in cursor:
        row.setValue("landId",id[0])
        row.setValue("buildingId",id[1])
        row.setValue("floorId",id[2])
        cursor.updateRow(row)
print("finish!")

你可能感兴趣的:(GIS)