I am just learning python and I need to convert a 2 digit numeric attribute in a table to a 2 digit zero-filled attribute in a script.
For example, 9 needs to be 09.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
If you already have a value and you need to make it 2 digit value, have a look:
import sys inputValue = sys.argv checkValue = int(inputValue) if checkValue <= 9: inputValue = '0' + inputValue print inputValue
input->output: 2 -> 02, 20 -> 20
This should do the trick (if using ArcGIS 10.1, for ArcGIS 10 you will have to use this update cursor):
First add a empty text field, e.g. textField to your feature class or shapefile.
You can run the below code in PythonWin, ArcMap, or ArcCatalog Python window. Or put it into a .py file and call it from a user created toolbox script tool.
import arcpy fc = "C://temp//Default.gdb//myLayer" # update to your layer fields = ('numField', 'textField') # put your numeric and text field here with arcpy.da.UpdateCursor(fc, fields) as cursor: for row in cursor: if row <= 9: row = '0' + str(row) cursor.updateRow(row)