0

I wrote an expression/codeblock that removes the words "Nearby:" from the field value string if it has it.

Codeblock:

import string
def splitme(s):
    if s[:7] == "Nearby:": 
        aList = string.split(s, ":")
        return aList[1]

    else:
        pass

Expression:

splitme(!Street1!)

It removes "Nearby:" on all fields that contain it in the string but it also deletes the strings in the field records that are fine and do not contain "Nearby:". I thought the if/else check would handle this.

  • 1
    in the else, you might try: return(s) instead of 'pass' – fluidmotion Jun 27 '15 at 21:52
  • Awesome. That did it! Reply with an answer and I'll mark this as answered. Thanks fluid! – TacoB0t Jun 27 '15 at 22:17
2

as written, the function returns a string with 'Nearby:' removed only if the 'Nearby:' test is true (if 'nearby:' exists within the field string. If the test is false, the function is set to 'pass', which returns no value (deleting the original value in the process)

to remedy, a value should be passed for both cases - true and false - something like

def splitme(s):
    if s[:7] == "Nearby:": 
        aList = string.split(s, ":")
        return aList[1]
    else:
        return(s) #return the original string if the test fails
        #pass
1

Assuming "Nearby:" is at the beginning of the line (and never in the middle), you can simplify this logic with a one-liner, without the need of a codeblock:

str(!Street1!).replace('Nearby:', '').lstrip()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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