Split one field into multiple based on a delimiter

10097
6
03-26-2013 07:44 AM
jasonheyman
New Contributor III
I am looking to split one field (BASE) into four based on a delimiter of ^ such as the following:

"PT COM 790 W INTER W LINE^B&S RR & S LINES SW LESS ^E200                     ^WEST"

I want

"PT COM 790 W INTER W LINE" to go to field "Line1"

"B&S RR & S LINES SW LESS" to go to field "Line2"

"E200                     " to go to field "Line3"

"WEST" to go to field "Line4"

I have searched online and have only seen splits based on X amount of space or only split once, not four times etc. Ex.
Line1 = !BASE!.split(" ", 1)[0]

Any help would be great, thanks.
Tags (2)
0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jason,

You can accomplish this with the following syntax in the field calculator:

!<field name>!.split("^")[0]


Replace <field name> with the field that contains the text.  The above would be for field "Line1".  For field "Line2" it would be:

!<field name>!.split("^")[1]


....and so forth. 

By default, the split method will split each occurrence of the specified character.  You just then need to call each one using it's index.

Also, be sure you have 'Python' checked at the top of the field calculator.
jasonheyman
New Contributor III
This does appear to work properly until I get to the second occurrence in the field. I assume since I have random spaces in front of the second and third lines, this is causing an issue.

additional samples of what my original data is like
ex1
"IRRG 1.13A PT COM 241.50' SE^  & 895.12'W INT N LINE SE 1/4 &^   CTR LINE SIMMON RD"

ex2
"DEER JUNCTION PLAT IV       ^LOT 7008                 ^                         ^"
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Are you looking to remove the "&" and spaces?  If so, I would recommend running the replace method on your initial field. Ex:

!<field name>!.replace("&", "")


You can then run the rstrip and lstrip methods to remove beginning and ending spaces in fields Line1, Line2, etc. after you run the split method.  Ex:

!<field name>!.rstrip()

!<field name>!.lstrip()
0 Kudos
jasonheyman
New Contributor III
Are you looking to remove the "&" and spaces?  If so, I would recommend running the replace method on your initial field. Ex:

!<field name>!.replace("&", "")


You can then run the rstrip and lstrip methods to remove beginning and ending spaces in fields Line1, Line2, etc. after you run the split method.  Ex:

!<field name>!.rstrip()

!<field name>!.lstrip()


No, the intent is to just split them into individual fields, and the code worked for the first word of the series, but when it hits the second word split the code fails.
0 Kudos
JeffPapirtis
New Contributor III
If I understand correctly you want to take one field "A" and create 4 new fields by splitting the contents of "A" based on the "^" symbol?  if this is a correct assumption you could do this individually with the following code:
PreLogic Code Block in Python:
def s(a):
   x = a.split('^')
   y = x[0]
   return y

B = s(!A!)

This creates a function called s().  (a) represents the field that I pass.  x = a.split('^') creates a list.  y = x[0] assigns the the first value in the list to y.  in your case it would be "PT COM 790 W INTER W LINE".

B= s(!A!) is the original field being pased to the function s()

in your next field you would change y = x[0] to y = x[1]

I think that this would be easier to automate if you created a stand alone script that accessed your fields by search Cursor, then depending on the length of your list assign it to the appropriate field using an update cursor.

Hope this point you in the right direction.

I have attached a photo of my field calculator with the code above.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
What's the error you receive when you try to execute the code on the second field?  You can see a more detailed explanation in the 'Results' window.  I could not reproduce this (see screen shots below).

[ATTACH=CONFIG]23155[/ATTACH][ATTACH=CONFIG]23156[/ATTACH][ATTACH=CONFIG]23157[/ATTACH]
0 Kudos