1

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 29, 2015 at 22:49
1
  • try using !SignatureDay!.zfill(2). That will pad single digits with a zero at the beginning. zfill is a python string method. Commented Jan 29, 2015 at 23:19

1 Answer 1

2

You do not need to do replace. You just need to modify the format string.

"Convert" = "{}{:02d}{:02d}".format(!SignatureYear!,!SignatureMonth!, !SignatureDay!)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jan 29, 2015 at 23:53
2
  • 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). Commented 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!)) Commented Jan 30, 2015 at 21:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.