I'm Working in Model builder using the calculate value tool. I have three columns - SignatureDay, SignatureMonth, SignatureYear - All three are string data types and all consist of just numbers for characters. I can combine most values easily into a single field and then convert that field into a date. My problem is that some values will not convert.
For instance:
SignatureDay SignatureMonth SignatureYear Convert SubmitDate
01 05 2014 20140105 1/5/2014
05 05 2014 20140505 5/5/2014
1 5 2014 201415
The code used to give me the values in the field
"Convert" = "{}{}{}".format(!SignatureYear!,!SignatureMonth!, !SignatureDay!)
for some reason the values with single digits and no zeroes in front, will not participate in the final output to a date type in the field "SubmitDate".
I've tried going through with the .replace() function but this table is dynamic and does not always have the same values. So -
!SignatureDay!.replace('1', '01').replace('2', '02')
....etc does not work, I would assume because all values 1-31(for every day possible) may not exist. Also has to be a better way than repeating that 31 times. I'm thinking there is an loop or something that could be run?
I'm a little lost on this one, my python experience is very limited as is my programming experience in general.
1 Answer 1
You do not need to do replace. You just need to modify the format string.
"Convert" = "{}{:02d}{:02d}".format(!SignatureYear!,!SignatureMonth!, !SignatureDay!)
-
This did not work received an error. ERROR 000539: Error running expression: "{}{:02d}{:02d}".format(u"2012",u"9", u"13") Traceback (most recent call last): File "<expression>", line 1, in <module> ValueError: Unknown format code 'd' for object of type 'unicode' Failed to execute (Combine Fields).Brock Steven– Brock Steven2015年01月30日 15:42:31 +00:00Commented Jan 30, 2015 at 15:42
-
OK. These are strings, not numbers. Then use the zfill. If these were numbers the code I posted would work, i.e.: "Convert" = "{}{:02d}{:02d}".format(!SignatureYear!,float(!SignatureMonth!),float( !SignatureDay!))Richard Fairhurst– Richard Fairhurst2015年01月30日 21:20:50 +00:00Commented Jan 30, 2015 at 21:20
Explore related questions
See similar questions with these tags.
!SignatureDay!.zfill(2)
. That will pad single digits with a zero at the beginning. zfill is a python string method.