Select to view content in your preferred language

Split text field into two new fields?

29895
17
11-24-2010 08:51 AM
DavidMedeiros
Frequent Contributor
Hi,

I'm a GIS MA student looking for some help on creating a Python script in ArcGIS for a class project. I have a text table with several fields (Tenant Name; Address; Sq Ft; Destination). The destination column contains information that I want to separate into two new fields - one for the first word in the original column and one for the second word in the original column. I then need to write this new info back into the original table.

What ArcPy commands should I be looking at to accomplish this? Can anyone point me to a similar finished script?

Thanks,

David
Tags (2)
0 Kudos
17 Replies
DavidMedeiros
Frequent Contributor
Ahh, I mistook last value for last whole word. Is there a modifier to the row.Type = row.Dest statement that can select the entire last word from the original row?
0 Kudos
DavidMedeiros
Frequent Contributor
Ok, I think I got it. Here's the script:

Script:
# split table script

#add field step
import arcpy

from arcpy import env
inputTable = arcpy.GetParameterAsText(0)
arcpy.AddMessage ("input " + inputTable)

#list fields (stops duplicate add fields)
theFieldList = arcpy.ListFields (inputTable, "Type")
arcpy.AddMessage ("the count" + str(len(theFieldList)))

#following exectues the add field
if len(theFieldList) == 0:
    arcpy.AddField_management(inputTable, "Type", "TEXT")
   
else:
    arcpy.AddMessage ("Field Exists")
   
#split field loop
rows = arcpy.UpdateCursor (inputTable)  #gets all vlaues for table

for row in rows:  #loops through vlaues and updates new field basedon last word of orig field
    row.Type = row.Dest.split(" ")[-1] if " " in row.Dest else ""
    rows.updateRow(row)

del row
del rows

resulting table has a new field "Type" with the second word from the "dest" row copied over. If no second word nothing is copied over. Thanks for the help all!
0 Kudos
charlieLatchford
Emerging Contributor
hi iam trying to do the same thing.. ive got a text field that contains an address each seperated with a "," i want to split into three new fields between the comers. for example

28, king road, new york

i want to be able to have the following field's
28
king road
new york

Does any one know any python script?
0 Kudos
NiklasNorrthon
Frequent Contributor
text = '28, king road, new york'
split_text = text.split(', ') # -> ['28', 'king road', 'new york']


A tip is to test the logic in the python interpreter before putting it together in a scirpt.
0 Kudos
MarkVolz
Frequent Contributor
Hello,

I am trying to split a name field into lastname and firstname.  The name field may or may not contain a last name.

examples:
"Anderson, Bill"
"CHS Company"

I am able to pull out the last name from the field.  However I am having trouble getting the first name (if there is any) out.

I tried the code:
!TAX_NAME!.split(",", 1)[-1] if "," in !TAX_NAME! else ""   however ArcGIS reported a syntax error
0 Kudos
BruceNielsen
Frequent Contributor
I don't use the CalculateField command very often (IMO UpdateCursors are easier to understand), but I think the syntax you're looking for is:
if ',' in !TAX_NAME! then !TAX_NAME!.split(',',1)[-1] else ""
0 Kudos
MarkVolz
Frequent Contributor
Bruce,

Thank you for your reply.

I am still getting an error:

ERROR 000539: Error running expression: if ',' in "PETERSEN,ERIC J & GAIL" then "PETERSEN,ERIC J & GAIL".split(',',1)[-1] else "" <type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1)
Failed to execute (Calculate Field (2)).
0 Kudos
BruceNielsen
Frequent Contributor
Like I said, I don't use this syntax very often. Here's the amended code:
if ',' in !TAX_NAME! then !NEW_FIELD!=!TAX_NAME!.split(',',1)[-1] else "".
0 Kudos