Is there any Python script which will add a field to shapefile which will be corresponding to first three characters from the shapefile name?
2 Answers
This code should do the trick. New field should be titled "New"
import arcpy
arcpy.env.workspace = r"c:\Temp"
shapename = "newshape.shp"
newfield = shapename[0:3]
arcpy.AddField_management(shapename,newfield,"TEXT","","","20")
-
-
1@Roy, This notation starts at and includes the first number, and ends at, but excludes, the last number. Commented May 10, 2012 at 16:54
-
1Thanks, indexing always gets me. Thank god for trial and error.– RoyCommented May 10, 2012 at 17:54
you can do this with Add Field (Data Management).
import arcpy
from arcpy import env
env.workspace = "C:/test.gdb"
inFeat = "cityoflondon.shp"
fieldLength = 10
#it will return "cit" from inFeat
fieldName = str(inFeat[:3])
arcpy.AddField_management(inFeat, fieldName, "TEXT", "", "", fieldLength)
-
I forgot to add that i need to add field in multiple shapefiles at once. I have one hundred shapefiles. Sorry, my mistake!– comCommented May 11, 2012 at 8:08