Select to view content in your preferred language

Script to Create New Label by Deleting after a comma & Splitting into 2 lines

2414
17
Jump to solution
05-16-2014 11:54 AM
TiaMorita
New Contributor III
I am using the ESRI CBSA feature class, and the names are super long, for example:

Anniston-Oxford-Jacksonville, AL Metropolitan Statistical Area

I want a VBscript or python script to use as a label expression to delete all words after the comma, and then stack the label into 2 lines, for example:

Anniston-Oxford-
Jackson


Any suggestions? Thanks!
Tags (2)
0 Kudos
17 Replies
TiaMorita
New Contributor III
Whoops left off a + sign


def FindLabel ( [Name] ):
  Split = [Name].split(",")
  Split = Split[0] #drops everything after comma
  Split = Split.split("-") #splits by the hyphen
  if len(Split) == 2:
    expression = Split[0] + "- \n" + Split[1] 
  elif len(Split) == 3:
    expression = Split[0] +"-" + Split[1] + "- \n" + Split[2]
  return expression



GIve that a shot.




Hi there - the results comes out empty 😞
0 Kudos
TiaMorita
New Contributor III
Here is some sample data to help out. Both of you are close, so hopefully this will help!
0 Kudos
IanMurray
Frequent Contributor
from your reply, you didn't use my most recent script, or at least you didn't post it back.


def FindLabel ( [Name] ):
  Split = [Name].split(",")
  Split = Split[0] #drops everything after comma
  Split = Split.split("-") #splits by the hyphen
  if len(Split) == 2:
    expression = Split[0] + "- \n" + Split[1] 
    return expression
  elif len(Split) == 3:
    expression = Split[0] + "-" + Split[1] + "- \n" + Split[2]
    return expression



Edit: Also, the attribute table for that file was empty.

Edit Again: NVM, I didn't unzip properly
IanMurray
Frequent Contributor

def FindLabel ( [Name] ):
  Split = [Name].split(",")
  Split = Split[0] #drops everything after comma
  Split = Split.split("-") #splits by the hyphen
  if len(Split) == 2:
    expression = Split[0] + "- \n" + Split[1] 
    return expression
  elif len(Split) == 3:
    expression = Split[0] + "-" + Split[1] + "- \n" + Split[2]
    return expression
  else:
    return Split[0]



Likewise mine labelled all the samples.  It will only work if there are 3 or less cities names in the name field.

I like yours Sol, except it splits single names in half, I think those ones would be fine on one line, but I guess its not my call.

[ATTACH=CONFIG]33885[/ATTACH]
TiaMorita
New Contributor III
It worked for me with the sample data you sent. 

Function FindLabel ([NAME])
pos = instr([NAME],"," )
lab =  Left([NAME], pos - 1)
leng = len(lab)
half = Int(leng/2)
first = left(lab,half)
digits = leng-half
nex = right(lab,digits)
 FindLabel = first & vbnewline & nex
End Function


Thanks, Sol


No - this was the result I got (see attached).
0 Kudos
TiaMorita
New Contributor III
 def FindLabel ( [Name] ):   Split = [Name].split(",")   Split = Split[0] #drops everything after comma   Split = Split.split("-") #splits by the hyphen   if len(Split) == 2:     expression = Split[0] + "- \n" + Split[1]      return expression   elif len(Split) == 3:     expression = Split[0] + "-" + Split[1] + "- \n" + Split[2]     return expression   else:     return Split[0] 


Likewise mine labelled all the samples.  It will only work if there are 3 or less cities names in the name field.

I like yours Sol, except it splits single names in half, I think those ones would be fine on one line, but I guess its not my call.


Actually - I think this it! Looks like it works! 🙂
0 Kudos
IanMurray
Frequent Contributor
Yeah, I agree splitting the text in half like that is not really nice, but the OP said that it didn't matter so I didn't make it more complicated.  Plus I'm racing against you to figure it out. lol 🙂  Plus I have to leave work now...


Haha, pretty much the same here.  From the two examples that were given I figured she wanted them to be split by the city names so thats what I went for.  Nothing wrong with yours, just a stylistic difference 😉

Edit:  Dang, she marked her quote of my response as answer, no answer provided for me 😞

😛
IanMurray
Frequent Contributor
Yours worked, it just wasn't quite stylistically what they wanted I suppose. 

Go home and have a good night!
0 Kudos