0

I'm trying to strip the last character in a string if when the string is split by spaces the last element has a length of 1. I then want to put that letter in front of the rest of the other elements.

So, if I had a value of 1st Street W, I would like to convert it to W 1st Street

I'm using this code in the "Pre-Logic Script Code" box in ArcMap field calculator:

def getPrefix(Street_Name):
  if len(!Street_Name!.split(" ")[-1])==1:
    !StreetName2! = !Street_Name!.split(" ")[-1] + " " + !Street_Name!.split(" ")[0:-1]

And in the box below that:

def getPrefix(!StreetName2!)

I'm getting an error 000989, syntax indention error, but I have everything at two spaces in cascading from calling the function. Pretty sure that's the ESRI specification for the field calculator.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
geoJshaun
  • 1,535
  • 1
  • 14
  • 31
  • 1
    You have at least 2 problems that I can see: you're defining your function twice (though the second one, on the last line, won't work), and you're telling your function to use the variable Street_Name but you're actually using !Street_Name! inside the function. – Dan C Oct 03 '17 at 17:37
  • @ Dan C Am I defining it twice, or passing the variable through it and calling the function in the code block? The last line is actually ina separate box of the field calculator GUI. – geoJshaun Oct 03 '17 at 17:42
  • @Dan C The way I understand it is that you define a function in the first block and here Street_Name is the parameter. In the second block, you call the function for the field update. In this case, !StreetName2! = def getPreFix(!StreetName2!), although I think it maybe should be def getPrefix(!Street_Name!). – geoJshaun Oct 03 '17 at 17:46
  • This answer should help: https://gis.stackexchange.com/questions/95267/error-000989-python-syntax-error-parsing-error-syntaxerror-invalid-syntax-li, or this one: https://gis.stackexchange.com/questions/8695/troubleshooting-error-000989-python-syntax-error-from-arcgis-field-calculator – Dan C Oct 03 '17 at 17:55
  • That did it. I just had to pass in the arguments correctly. Used this in the prelogic--- def getPrefix(Street_Name, StreetName2): if len(Street_Name.split(" ")[-1])<2: StreetName2 = '{} {}'.format(Street_Name.split(" ")[-1],Street_Name.split(" ")[0:-1]) return StreetName2 --- and this in the box below --- getPrefix(!Street_Name!, !StreetName2!) --- my results are not the way I want. Coming out as W [u'1st', u'Street'] instead of 'W 1st Street'. – geoJshaun Oct 03 '17 at 19:04
  • Instead of
    StreetName2 = '{} {}'.format(Street_Name.split(" ")[-1],Street_Name.split(" ")[0:-1])
    Use
    StreetName2 = '{} {}'.format(Street_Name.split(" ")[-1],' '.join(Street_Name.split(" ")[0:-1]))
    EDIT - sorry I have no idea how to make it enter on a different space. Also, added join instead of *.
    – isamson Oct 03 '17 at 19:54
  • That kind of works, but I am not getting the last element. Coming out as "W 1st" Instead of "W 1st Street" – geoJshaun Oct 03 '17 at 19:59
  • Sorry, edited to do ' '.join, try that instead. – isamson Oct 03 '17 at 20:00
  • @isamson getting a general error with ''.join – geoJshaun Oct 03 '17 at 21:29

0 Answers0