4

I have three text fields populated as follows: 1, 9, 1 that I need to concatenate into a field that looks like 01.09.01. Getting to 1.9.1 is easy but can anyone help out with how to formulate it so that each value has two places (i.e. add in those zeroes). I used to do this in Excel but can't figure out how to work in ArcMap 10.

4

Use the zfill() command in Python. Set your Field Calculator parser to Python, and do this:

str(!Field1!).zfill(2)
2
"0"& [field1]&".0"& [field2] & ".0" & [field3]  

This comment makes me start thinking of regex, (if you Must have a hard field with all these values) I am not too good with regex but I know it can fix this. not sure what the use case is.

  • 1
    Erm, what if there is a number larger than 9? – blah238 Nov 30 '12 at 22:14
  • Don't think regex is the answer here. – blah238 Dec 1 '12 at 0:11
0

Generally, I do something like the following in Python, to pad with leading '0's to whatever length is needed:

while len(yourString) < 5:
    yourString = '0' + yourString
0

Here is a simple Python function that right-justifies to the specified width each item in a sequence of items using rjust and then joins them using join. A list comprehension is used to keep it short and simple. The optional arguments default to your specifications.

def padjoin(sequence, width=2, sep='.', fillchar='0'):
    return sep.join([str(item).rjust(width, fillchar) for item in sequence])

Example usage:

>>> padjoin([1, 9, 1])
'01.09.01'
>>> padjoin([1, 23, 45])
'01.23.45'

Field calculator expression (be sure to switch to the Python parser):

padjoin([str(!field1!), str(!field2!), str(!field3!)])

The str() conversion function is needed for each field reference in case there are any null values as the default field calculator behavior when a null is encountered is to stop execution and skip to the next row.

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.