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?

enter image description here

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),

enter image description here

but even the "field" is as TEXT, I have an error:

enter image description here

I have no experiences with Python, thus I prefer ModelBuilder.

  • yes, I modified my question – maycca Dec 8 '16 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" too – maycca Dec 8 '16 at 13:03

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.