Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have been racking my brain, but I can't seem to find a solution to appending my field values in my .dbf to correspond with the values of my feature's attribute table. All I need to do is 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.

share|improve this question
    
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 '12 at 10:07
add comment

3 Answers 3

You have several options in python: zfill, interpolation operator ('%'), and rjust (pad with any character...not just zeros). See this SO question: http://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

share|improve this answer
add comment

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!)
share|improve this answer
add comment

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! )
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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