1

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?

asked Oct 1, 2019 at 14:15
2
  • 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. Commented Oct 1, 2019 at 14:27
  • Yes. they are start with S. But I don't know how to write the expression to do this Commented Oct 1, 2019 at 14:41

2 Answers 2

1

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

answered Oct 1, 2019 at 14:41
6
  • can i run this in Field Calculator? Commented Oct 1, 2019 at 14:54
  • Yes. Be sure to select the Python interpreter option and replace old_string with the attribute name. Commented Oct 1, 2019 at 14:57
  • I post my Field Calculator setting.I cannot run your code and I get error. Commented Oct 1, 2019 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. Commented Oct 1, 2019 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. Commented Oct 1, 2019 at 17:06
1

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:

arcpy.GetInstallInfo()['SourceDir'][:40]

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)

answered Oct 4, 2019 at 0:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.