I have a text field, field1
, that's populated with 14-digit identifiers, like "01234567890123"
.
I create a new field, field2
, that I want to show the first four digits of field1
.
I would like to do this using arcpy.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI have a text field, field1
, that's populated with 14-digit identifiers, like "01234567890123"
.
I create a new field, field2
, that I want to show the first four digits of field1
.
I would like to do this using arcpy.
kind of figured it out as soon as I posted. Figured I should post the answer...
arcpy.CalculateField_management(myFeatureLayer, field2, "!field1![0:4]", "PYTHON")
suppose I should add that one can apply any standard Python string functions to !field1!
and that this syntax can be used in the Field Calculator if one toggles the Python radio button to the on position.
Here is an Update Cursor approach:
import arcpy
shp = r'C:\path\to\your\shapefile.shp'
with arcpy.da.UpdateCursor(shp, ["field1", "field2"]) as cursor:
for row in cursor:
row[1] = row[0][:4]
cursor.updateRow(row)