I have list of numbers like this (S20170146) and I want to turn them to this format(S2017-01-46).Basically, I need to add "-" between the numbers. I am using Arcmap I know how to use feild calculator. But I don't know to write python code to do this?

  • Are the segments of the string always the same length? S(4 digits)-(2 digits)-(2 digits)? If so, it would be pretty easy to write an expression to grab the right chunks and then concatenate them with dashes. – lambertj Oct 1 at 14:27
  • Yes. they are start with S. But I don't know how to write the expression to do this – Kylie Oct 1 at 14:41

You can use slicing along with string formatting to accomplish this:

new_string = '{}-{}-{}'.format(old_string[:5], old_string[5:7], old_string[7:])

Explanation of the 3 slices:

  1. The first 5 elements of the sequence (index 0-4)
  2. 2 elements, index 5 & 6
  3. All remaining elements (index 7-end)

This page gives a decent explanation on slicing strings

  • can i run this in Field Calculator? – Kylie Oct 1 at 14:54
  • Yes. Be sure to select the Python interpreter option and replace old_string with the attribute name. – Bjorn Oct 1 at 14:57
  • I post my Field Calculator setting.I cannot run your code and I get error. – Kylie Oct 1 at 15:30
  • Try removing those spaces between the attribute name and the [] brackets to be safe. You also need to actually calculate the value to new_string: replace thing(!LAST_allpaving_!) with new_string. – Alex Oct 1 at 15:41
  • You don't need to use the codeblock. Just paste in everything to the right of the '=' from my expression, and make the changes @Alex suggests. – Bjorn Oct 1 at 17:06

pre-logic code block might work better as:

def thing(last_allpaving):
  return '{}-{}-{}'.format(last_allpaving[:5], last_allpaving[5:7], last_allpaving[7:])

as stated in some comments, it is not necessary to use a pre-logic code block, but if you choose to do so, this should help.

In general...

When using The Field Calculator with a Python expression, the 'expression' part is said to be the value that you set your field equal to. This expression has to be something that Python can parse like a constant or a built-in pre-defined function like:


as shown here:

enter image description here

An Advanced Python Expression implies we want to run our own pre-processing function which is previously un-defined in order to arrive at the calculated value. For this, we need to check the Show Codeblock option box.

enter image description here

In the Pre-Logic Script Code text area, we can enter our user-defined python function

def my_new_function(field_data):
  x = field_data
  y = x[:8]
  z = y[5:]
  return z

and in the Expression text area, we are now able to call our new user-defined function using

my_new_function( !LOC_DATA! )

Care must be taken to ensure that the value returned by your user-defined function is a type that is compatible with the field that you are calcualting. (ie. I wouldn't recommend trying to put a String into an Integer field)

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.