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– lambertj2019年10月01日 14:27:41 +00:00Commented Oct 1, 2019 at 14:27
-
Yes. they are start with S. But I don't know how to write the expression to do thisKylie– Kylie2019年10月01日 14:41:20 +00:00Commented Oct 1, 2019 at 14:41
2 Answers 2
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:
- The first 5 elements of the sequence (index 0-4)
- 2 elements, index 5 & 6
- All remaining elements (index 7-end)
-
can i run this in Field Calculator?Kylie– Kylie2019年10月01日 14:54:41 +00:00Commented Oct 1, 2019 at 14:54
-
Yes. Be sure to select the Python interpreter option and replace
old_string
with the attribute name.Bjorn– Bjorn2019年10月01日 14:57:03 +00:00Commented Oct 1, 2019 at 14:57 -
I post my Field Calculator setting.I cannot run your code and I get error.Kylie– Kylie2019年10月01日 15:30:20 +00:00Commented 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.Alex– Alex2019年10月01日 15:41:38 +00:00Commented 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.Bjorn– Bjorn2019年10月01日 17:06:29 +00:00Commented Oct 1, 2019 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:
arcpy.GetInstallInfo()['SourceDir'][:40]
as shown 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.
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)
Explore related questions
See similar questions with these tags.