4

I am trying to populate a field with the last word in an address string using field calculator within ModelBuilder. My code takes the whole address string (1234 S Brittany Town Dr) as a parameter, then splits it into a list and returns the last item in the list:

def getSufDir( prop_location ):
    prop_location.strip()
    x = prop_location.split(' ')
    return x[len(x)-1]

This code works for 433 of the 37,000 records in the layer. I discovered that those 433 records have 25 characters (prop_location is a 25 character maximum field). All other records return blank. Why is this happening? These code snippets seem to work fine in IDLE, but return blank values for most records when done in Field Calculator. I have attached an image of my field calculator window for reference.

enter image description here

An example of an address that is failing to process is 6847 W Mesa Arch Dr.

I edited to code to:

return x[-1] 

and got an identical result.

  • 4
    What happens if you use 'return x[-1]'? This will also return the last item in the list. – Bjorn May 10 '16 at 21:31
  • 7
    try creating a new variable as the result of the strip() method. so s = prop_location.strip() and then proceed by using s. strip does not modify the object (like sort() does with a list) but it returns a new object. – mr.adam May 10 '16 at 22:30
  • 2
    x[-1] should do the job. also check if records are within environment spatial extent – FelixIP May 10 '16 at 22:50
  • 1
    Is it possible that the field is filled with spaces at the end of the string? And the blank it is returning is just a space? The 433 rows that are a full 25 characters wouldn't have spaces at the end of the string. – Fezter May 11 '16 at 0:37
  • 6
    I think @mr.adam's comment will solve your problem. Further I would advise using prop_location.strip().split(" ")[-1] in the Expression without bothering the Code Block. – fatih_dur May 11 '16 at 1:07
1

There were some records that were blank which caused an error. This code works:

def getSufDir( prop_location ):
if prop_location is None:
    return None
else:
    return prop_location.strip().split(" ")[-1]
  • 2
    Just a style comment, you don't need to specify a space in split(); a space is the default. – recurvata May 11 '16 at 16:35

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.