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.

  • 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 3 '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 3 '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 3 '17 at 17:46
  • This answer should help: gis.stackexchange.com/questions/95267/…, or this one: gis.stackexchange.com/questions/8695/… – Dan C Oct 3 '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 3 '17 at 19:04