1

I'm trying to calculate records in a text field to "01", keeping the preceding zero in tact using python script in ArcGis.

I can do this easily using the field calculator in ArcMap or the calculate field gp tool. But the script results in "1" instead of "01" and I can't figure out how to fix it.

arcpy.CalculateField_management("Table1","Field1","01","PYTHON","#")
1
  • Please do not include thanks in your posts here. It is considered unnecessary chit chat as per the Tour and help center.
    – PolyGeo
    May 20, 2016 at 20:02

2 Answers 2

1

Try putting your number "01" into a second set of quotes "'01'" (double-quote then single-quote or the other way around)

arcpy.CalculateField_management("Table1","Field1","'01'","PYTHON","#")
1

Try using the .zfill() function somewhere in your code. Input the length you are needing in the parentheses, 2 in your case. This will pad the text field with 0s

2
  • Could you please include an example of how to use .zfill()?
    – Midavalo
    May 20, 2016 at 20:48
  • When i need to pad a field with 0's, say a ZIP code that is 00601, but displaying as 601. !ZIP!.zfill(5) would field calculate 2 0's in front, turning it into 00601
    – Maksim
    May 20, 2016 at 20:57

Your Answer

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

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