Hello all,
I have a text field (i.e. DUE_TIME) and I need to split off the last value into a new field (i.e. VERSION). I have a script that works to this end, but I cannot incorporate it into modelbuilder (i.e. lack the skills currently) and as I am short on time, I have shelved that for now and am hoping to simply use the field calculator. The dilemma I'm having is that the field I'm trying to split has inconsistent indexing (i.e. month/day/year or month/day/year #) so trying to simply use the split function in the field calculator throws an error "index is out of range". Is there a way to get around this by using the code block in the field calculator? An If-Then Statement perhaps? My goal is to populate the VERSION field with the number after the date (in the DUE_TIME field) IF a number exists after the date, and otherwise, populate "0". Thank you all very much for your time and consideration.
Solved! Go to Solution.
code block python parser, replace 'fld' with !YourFieldName!
def spl(fld):
s = fld.split(" ")
if len(s) > 1:
return s[1]
else:
return 0
# examples
fld = "2015/12/12"
spl(fld)
0
fld = "2015/12/12 2"
spl(fld)
'2'
If you need integers or whatever, 'int' the return portion
Dan,
Thank you very much for the quick response, and for all of the answers you've provided in the ESRI community (you've kept me from losing my hair more than once). That being said, I'm still having trouble with this one.... I've applied your code in the code block, the expression validates, and I hit "apply", but still get an index error. Both the DUE_TIME and VERSION fields are text fields. I removed the exclamation points from the code block because they were throwing a syntax error. Am I missing something?
Try
spl(!DUE_TIME!)
in the box where you currently have !DUE_TIME!.split(" ")[1]
and Dan's solution should work
code block python parser, replace 'fld' with !YourFieldName!
def spl(fld):
s = fld.split(" ")
if len(s) > 1:
return s[1]
else:
return 0
# examples
fld = "2015/12/12"
spl(fld)
0
fld = "2015/12/12 2"
spl(fld)
'2'
If you need integers or whatever, 'int' the return portion
Dan,
Thank you very much for the quick response, and for all of the answers you've provided in the ESRI community (you've kept me from losing my hair more than once). That being said, I'm still having trouble with this one.... I've applied your code in the code block, the expression validates, and I hit "apply", but still get an index error. Both the DUE_TIME and VERSION fields are text fields. I removed the exclamation points from the code block because they were throwing a syntax error. Am I missing something?
Try
spl(!DUE_TIME!)
in the box where you currently have !DUE_TIME!.split(" ")[1]
and Dan's solution should work
This worked perfectly! Thank you Dan! Thank you Alfred!