I have a column of strings that always start with the first two characters being numbers, like 39 or 07, etc. In a new column I want to process the original column so that I replace any of those first two numbers that are not 39 with a 39, but making sure the remaining trailing string characters are still there. So a find and replace but index on the first two characters. I want to use the .replace() command but not sure how to tie in the index, replace command and an If Than Else statements to make it work.
In field calculator it would be like this:
replace(!Permit_num!)
code block:
import re
def replace(val):
if (val[0:1] == "07"):
return re.sub ('07','39'+val[2:], val)
elif (val[0:1] == "10"):
return re.sub ('10','39'+val[2:], val)
elif (val[0:1] == "11"):
return re.sub ('11','39'+val[2:], val)
elif (val[0:1] == "19"):
return re.sub ('19','39'+val[2:], val)
elif (val[0:1] == "24"):
return re.sub ('24','39'+val[2:], val)
elif (val[0:1] == "27"):
return re.sub ('27','39'+val[2:], val)
elif (val[0:1] == "34"):
return re.sub ('34','39'+val[2:], val)
elif (val[0:1] == "35"):
return re.sub ('35','39'+val[2:], val)
elif (val[0:1] == "39"):
return re.sub ('39','39'+val[2:], val)
elif (val[0:1] == "50"):
return re.sub ('50','39'+val[2:], val)
elif (val[0:1] == "54"):
return re.sub ('54','39'+val[2:], val)
elif (val[0:1] == "56"):
return re.sub ('56','39'+val[2:], val)
elif (val[0:1] == "99"):
return re.sub ('99','39'+val[2:], val)
I tried the above, but does not work, get a 99999 error and says my field I am calculating into is not nullable. The new field is also a string.
return '39' + val[2:]
should be enough? No need for ifs or re