Adding zeros or numbers in a Field

4972
26
Jump to solution
10-24-2014 01:32 PM
MichelleCouden1
Occasional Contributor III

I have two fields that I need to concatenate together but I need to add zeros so one said has 4 digits and the other side has 2 digits. Both separated with a hypen. For Example, 0024-14.

 

This is the code I have written am I close???

Left([ACONTROL], 4, "-") & Right([ASECTION], 2)

 

So, it would be field ACONTROL with 4 digits and ASECTION with 2 digits.

0 Kudos
26 Replies
DanPatterson_Retired
MVP Emeritus

OMG  you might be using an older version of Arcmap and not Python 2.7 or above

try this

"00{0}-0{1}".format(int(!ACONTROL!)),int(!ASECTION!))

Notice the {0} instead of {} and {1} instead of {}

grief...if that is it...note to both of ourselves...arcmap and python version are needed

0 Kudos
MichelleCouden1
Occasional Contributor III

WWHHHooooaaaa!!! That was it. Sorry, we didn't think of that earlier. I'll have to remember that for future questions. Thank you again for all your help and being patient.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I have added it to my notes...in updated versions of python the position number within the {} brackets isn't needed, however, I have not learned, that the extra characters {0} and {1} go a long way to simplifying problems...be overt...not covert

0 Kudos
MichelleCouden1
Occasional Contributor III

I am also trying print "%s-%s" % (!ACONTROL!.zfill(4), !ASECTION!.zfill(2)). It also contracts a syntax error.

Do I have to put anything before the print?

Should it be

(period) .print "%s-%s"% .......

0 Kudos
MichelleCouden1
Occasional Contributor III

Do I put something in place of the % marks? I am getting a syntax error.

0 Kudos
BruceNielsen
Occasional Contributor III

I noticed that your original fields are integer. In that case it would be (using the newer syntax):

     print "{0}-{1}".format(str(!ACONTROL!).zfill(4), str(!ASECTION!).zfill(2))

0 Kudos
DanPatterson_Retired
MVP Emeritus

Amy check the last line...I want to ensure that you had integers...I edited the good post just to be careful

Bruce...zfill is great if the size of the string is fixed and you want to get some 0's added on, but it will fail in many cases since you need to change the zfill(number) to accommodate...for example see

>>> a = 3653.000

>>> b = 1.000

>>> "{0}-{1}".format(str(a).zfill(6), str(b).zfill(4))

'3653.0-1.0'

>>> "{0}-{1}".format(str(a).zfill(8), str(b).zfill(6))  #doesnt

'3653.0-01.0'

>>> "{0}-{1}".format(str(a).zfill(8), str(b).zfill(6))  #doesn't

'003653.0-0001.0'

>>> "{0}-{1}".format(str(int(a)).zfill(6), str(int(b)).zfill(2)) # sort of works

'003653-01'

>>> "{0}-{1}".format(str(a).zfill(8), str(b).zfill(6))  #doesnt

'003653.0-0001.0'

>>> "00{0}-0{1}".format(int(a),int(b))        #sort of works

'003653.0-01.0'

>>> "00{0}-0{1}".format(int(a),int(b))        #works for floats

'003653-01'

>>>

I hate it when table decimals don't show even though create them in the first place

0 Kudos