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

905
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
1 Solution

Accepted Solutions
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! :)

View solution in original post

0 Kudos
17 Replies
TiaMorita
New Contributor III
Thanks, but all that does is get rid of the comma and then stack the label. What I want to do is get rid of all the text after the comma (including the comma) and stack the label.

For example, if the name is "Atlanta-Sandy Springs-Roswell, GA Metropolitan Statistical Area"

I want to read something like:

Atlanta-Sandy
Springs-Rosewell
0 Kudos
IanMurray
Frequent Contributor
This may work, it will depend on how many names come before the comma.


This should drop everything after the comma, then put the first two cities on top with the 3rd city beneath, if there are only two cities hyphenated, then it will stacked them one atop another.

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


Make sure you use python and advanced expression.

Unfortunately I have no data to test on but hopefully this does it.
0 Kudos
TiaMorita
New Contributor III
Thanks for giving it a shot. Unfortunately, I get an error message: "The expression contains an error. Modify the expression and try again. Error 0 on line 0. SyntaxError: invalid syntax (<string>,line6)."

the field name I'm working with is [name].

I don't really care where the words get split up so long as it creates 2 lines (maybe so long as it gets split after the second word/name?) Also, some of the names have hyphens between each name and others not. For example:

Albany-Schenectady-Troy, NY Metropolitan Statistical Area
Virginia Beach-Norfolk-Newport News, VA-NC Metropolitan Statistical Area
Tampa-St. Petersburg-Clearwater, FL Metropolitan Statistical Area



Thanks for the help!!!
0 Kudos
IanMurray
Frequent Contributor
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.
0 Kudos
TiaMorita
New Contributor III
Still not working :(
See attached error msg
0 Kudos
IanMurray
Frequent Contributor
lol this would be alot easier if I had the dataset and could debug myself.  I tend to make a few errors when writing scripts

I needed to put the return expression within the if/elif statement I believe


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
0 Kudos
TiaMorita
New Contributor III
The result looks like this...
0 Kudos
TiaMorita
New Contributor III
I was looking for a script that deletes all the info after the comma, and then splits the City name into 2 lines.
0 Kudos
IanMurray
Frequent Contributor
lol this would be alot easier if I had the dataset and could debug myself.  I tend to make a few errors when writing scripts

I needed to put the return expression within the if/elif statement I believe


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