Calculating field, pad zeros to a max field width of 6

5366
8
Jump to solution
05-02-2012 11:45 AM
MichaelMiller2
Occasional Contributor III
I am trying to calculate a field value so that I pad 0's to the existing values to a max width of 6 chars.

Being a python newbie, I'm a little shaky with coding. I've started with the following code and getting nowhere. Any help/direction is appreciated

arcpy.CalculateField_management(outCrash, "Segment_Code", !Segment_Code!.ljust(!Segment_Code!,6,0), "PYTHON")


Michael Miller
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Sorry to be a pest with this simple task, but still not working. Appears that is is treating the expression as the field.......confused.

It now returns the following error:

ERROR 000728: Field str(!Segment_Code!).zfill(6) does not exist within table
Failed to execute (CalculateField).


You are using the wrong syntax. Check your tool documentation here. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

The expression is the third variable passed to the tool, the field is the second.

View solution in original post

0 Kudos
8 Replies
DarrenWiens2
MVP Honored Contributor
Calculator Expression:
str(!Segment_Code!).zfill(6) 
0 Kudos
MichaelMiller2
Occasional Contributor III
Calculator Expression:
str(!Segment_Code!).zfill(6) 


Thanks Darren.

It's now returning a syntax error.......invalid syntax on the exp=.... line.

            exp = str(!Segment_Code!).zfill(6)
            arcpy.CalculateField_management(outCrash, exp, "PYTHON")
0 Kudos
DarrenWiens2
MVP Honored Contributor
I think you just need quotes:
exp = "str(!Segment_Code!).zfill(6)"
arcpy.CalculateField_management(outCrash, exp, "PYTHON")
0 Kudos
MichaelMiller2
Occasional Contributor III
I think you just need quotes:
exp = "str(!Segment_Code!).zfill(6)"
arcpy.CalculateField_management(outCrash, exp, "PYTHON")



Sorry to be a pest with this simple task, but still not working. Appears that is is treating the expression as the field.......confused.

It now returns the following error:

ERROR 000728: Field str(!Segment_Code!).zfill(6) does not exist within table
Failed to execute (CalculateField).
0 Kudos
MathewCoyle
Frequent Contributor
Sorry to be a pest with this simple task, but still not working. Appears that is is treating the expression as the field.......confused.

It now returns the following error:

ERROR 000728: Field str(!Segment_Code!).zfill(6) does not exist within table
Failed to execute (CalculateField).


You are using the wrong syntax. Check your tool documentation here. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

The expression is the third variable passed to the tool, the field is the second.
0 Kudos
MichaelMiller2
Occasional Contributor III
You are using the wrong syntax. Check your tool documentation here. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

The expression is the third variable passed to the tool, the field is the second.



Duh.....Thanks for pointing that out Mathew.

Michael
0 Kudos
MichaelMiller2
Occasional Contributor III
I think you just need quotes:
exp = "str(!Segment_Code!).zfill(6)"
arcpy.CalculateField_management(outCrash, exp, "PYTHON")


Thanks for the suggestions Darren. The script now runs, but is not adding the 0's to the left side of the Segment_Code values. Any gotchas that I'm missing here?

Michael
0 Kudos
MichaelMiller2
Occasional Contributor III
Thanks for the suggestions Darren. The script now runs, but is not adding the 0's to the left side of the Segment_Code values. Any gotchas that I'm missing here?

Michael


Just discovered the Segment_Code field was converted to numeric during the TableToTable conversion. Looks like I'll have to figure out a method to preserve the string field type before I can pad the 0's.

Thanks all for the help.

Michael
0 Kudos