3

How do I append my field values in my .dbf to correspond with the values of my feature's attribute table.

I need to append 0's to the field value if the field value is less than 11 characters in length.

It needs to be done in the field calculator through VB Script or Python.

1
  • Can you give more details please? are the 0s to be added in front or behind the numbers? Why are you referring to dbf files if you want to work in field calculator? Do you want the new values to be strings or integers?
    – sgrieve
    Dec 3, 2012 at 10:07

3 Answers 3

2

You have several options in python: zfill, interpolation operator ('%'), and rjust (pad with any character...not just zeros). See this SO question: https://stackoverflow.com/questions/339007/python-nicest-way-to-pad-zeroes-to-string

There's also an example here: http://forums.arcgis.com/threads/35137-Populate-a-field-with-consecutive-numbers-by-a-constant-length-string.?p=119553#post119553

1

Using python's string multiplication, this can be done very simply in a couple of lines. The only tricky bit is that you must convert an int to a string in order to use the len() function.

Assuming the field you are working with is called x:

def pad(x):
 if len(str(x)) < 11:
  return str(x) + ('0'*(11-len(str(x))))

Which can be called with:

pad(!x!)
0

possibley this could be a one-liner..but i am not able to figure out for now :)

Pre-Logic Script Code:

def AppendZeroes(strContent):
    a = len(strContent)
    strTemp = ""
    if(a < 11):
        for i in range(11- a):
            strTemp =  strTemp + "0"
        strContent = strContent + strTemp
    return strContent

call it using

AppendZeroes( !YOUR_FIELD_NAME! )

Your Answer

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

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