3

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.

1
  • return '39' + val[2:] should be enough? No need for ifs or re
    – BERA
    Sep 6, 2019 at 5:14

2 Answers 2

1

You can use a list comprehension to index and replace the first characters for each item in a list. For example:

test = ["3901","1420","2270","3985"]

def replace(val):
    new = ["39" + i[2:] if i[:2] != "39" else i for i in test]     
    print (new)

replace(test)
1

I finally figured out the problem. Was my index position. So this type of code works as well.

permit_fix =

replace(!permit_num!)

Code Block

import re

def replace(val):

    if (val[0:2] == "07"):

        return re.sub ('07','39', val)

    elif (val[0:2] == "10"):

        return re.sub ('10','39', val)

    elif (val[0:2] == "11"):

        return re.sub ('11','39', val)

    elif (val[0:2] == "19"):

        return re.sub ('19','39', val)

    elif (val[0:2] == "24"):

        return re.sub ('24','39', val)

    elif (val[0:2] == "27"):

        return re.sub ('27','39', val)

    elif (val[0:2] == "34"):

        return re.sub ('34','39', val)

    elif (val[0:2] == "35"):

        return re.sub ('35','39', val)

    elif (val[0:2] == "39"):

        return re.sub ('39','39', val)

    elif (val[0:2] == "50"):

        return re.sub ('50','39', val)

    elif (val[0:2] == "54"):

        return re.sub ('54','39', val)

    elif (val[0:2] == "56"):

        return re.sub ('56','39', val)

    elif (val[0:2] == "99"):

        return re.sub ('99','39', val)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.