I have a series of polygon data. From each shapefile, I want to select the polygons with some condition (bb = 3).
To new shapefiles, I added a new field: "year". I want to fulfill the "year" field with:
shp1 -> 2006 shp2 -> 2007 shp3 -> 2008 ... shp11-> 2014
I used ModelBuilder to iterate through shapefiles, make selection and add a new "year" field.
However, how can I calculate the year field by 2006,2007,...2014 values in iteration mode?
Or, how to at least insert in year Field unique values? I tried to insert there my %name% variable (as mlc_2006, thus not the number but a text),
but even the "field" is as TEXT, I have an error:
I have no experiences with Python, thus I prefer ModelBuilder.
-
yes, I modified my questionmaycca– maycca2016年12月08日 12:41:43 +00:00Commented Dec 8, 2016 at 12:41
-
cooll !! that was quick fix ! can you please post this as an answer, that I can accept it? and I set my expression as "PYTHON_9.3" toomaycca– maycca2016年12月08日 13:03:00 +00:00Commented Dec 8, 2016 at 13:03
1 Answer 1
If the value you want to use to populate the field is a part of the feature class' name, you can use the string indexes of the characters you want to retrieve.
Don't forget to wrap the expression with double quotes and choose Python as parser in the Calculate Field interface.
For example,
"%Name%"[-3:]
will return the last 3 characters"%Name%"[0:2]
will return the first (index 0) to the third (index 2) character, not included
You'll find a ton of examples of slicing strings with Python using their indexes on Google, see e.g. here.
Explore related questions
See similar questions with these tags.