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.

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

2 Answers 2

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 and acknowledge you have read our privacy policy.

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