Skip to content

Instantly share code, notes, and snippets.

@HerbFargus
Created March 14, 2017 22:23
Show Gist options
  • Save HerbFargus/0e9275f2b3ab7744c5e04419d0bdbfee to your computer and use it in GitHub Desktop.
Save HerbFargus/0e9275f2b3ab7744c5e04419d0bdbfee to your computer and use it in GitHub Desktop.
Arcmap Field Calculator (Python): Add leading zeroes to single character values (1-9)
Say I have a list of sections, out of the 36 numbers I want 1-9 to have the format 01,02, etc.
They are currently ints but my final field will be a text value so I need to convert the int to a text value to add a preceding 0.
Add a new field
use field calculator to fill the column based on the int, this will now be a text value.
Then run the following script in the field calculator:
Pre-Logic Script Code:
def addzero(f1, f2):
if len(f1) == 1:
return "0" + f1
else:
return f1
Field =:
addzero( !FIELD1!, !FIELD2!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment