arcpy.da.UpdateCursor to concatenate two strings

4671
10
Jump to solution
10-21-2013 09:57 AM
ClintonCooper1
New Contributor III
I am trying to figure out how to concatenate two strings using the arcpy.da.UpdateCursor method (newbie to python scripting).  I have tried many trial and error attempts in the basic code from resource center, and have yet to get one that will work.  Here is the code I am working with:

import arcpy  fc = " C:\Users\cc1\Desktop\Metro_Bus_Stops.shp" fields = ('City','TRIAL')   with arcpy.da.UpdateCursor(fc, fields) as cursor:  in cursor:         row[0] = row[1]         cursor.updateRow(row)  


This code works fine for updating a new field with the current data form another field.  But I cannot get it to concatenate two fields together for the new third field.  Also, I have seen some literature with the ''.join() method, that I am interested in.  I am working with over 7 million records, and every once of performance I can gain is needed.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Here's a quick example. Takes field0 and field1 and writes them to field2.

fields = ['field0', 'field1', 'field2'] cursor = arcpy.da.UpdateCursor(fc, fields) for row in cursor:     row[2] = '{0}{1}'.format(row[0], row[1])     cursor.updateRow(row) del cursor

View solution in original post

0 Kudos
10 Replies
MathewCoyle
Frequent Contributor
Here's a quick example. Takes field0 and field1 and writes them to field2.

fields = ['field0', 'field1', 'field2'] cursor = arcpy.da.UpdateCursor(fc, fields) for row in cursor:     row[2] = '{0}{1}'.format(row[0], row[1])     cursor.updateRow(row) del cursor
0 Kudos
ClintonCooper1
New Contributor III
do I change format to join?  Thank you for the quick reply!!
0 Kudos
MathewCoyle
Frequent Contributor
That depends on what you want your output to be. What reason would you have for using join in this context?
0 Kudos
ClintonCooper1
New Contributor III
I am trying to concatenate two fields, and the literature I have found says that .join is the solution to concatenate the two fields.  When I run the script you provided with my information, I am getting this error:

Runtime error
Traceback (most recent call last):
  File "<string>", line 8, in <module>
IndexError: list assignment index out of range

for this code:


import arcpy

fc = " C:\Users\cc1\Desktop\Metro_Bus_Stops.shp"

fields = ['City', 'County', 'TRIAL']
cursor = arcpy.da.UpdateCursor(fc, fields)
for row in cursor:
    row[3] = '{0}{1}'.format(row[0], row[1])
    cursor.updateRow(row)
del cursor


Clinton
0 Kudos
MathewCoyle
Frequent Contributor
Ah yes I mixed up the index for the 3rd field. Should be this.
    row[2] = '{0}{1}'.format(row[0], row[1])


And what literature are you referring to?
0 Kudos
ClintonCooper1
New Contributor III
It seems that the .join is an older method.  In your estimation, will this be the fastest method to concatenate strings (aka faster than field calculator)?   Or is  there another method that will be much faster?  As I have said in previous posts, I need to do this calc on 7+ million records...through field calculator this takes 24 hours....  Thanks again for all your help!!!
0 Kudos
MathewCoyle
Frequent Contributor
Assuming I/O isn't your issue this method should take less than 2 minutes for 7 million records (takes around 10 seconds for 500k records for me). The only faster way I can think of is it do this through SQL directly in a database.
0 Kudos
ClintonCooper1
New Contributor III
ran through it last night, and it took around 10 to 15 minutes for a shapefile.  I then ran it again on a table (hoping to skip the geocoding process), and it took around 30 to 45 minutes.  Can't complain as this is about 10 to 20 times faster than field calculator.  I may eventually look into doing it through sql, but for now this will suffice!  Thanks again!!!
0 Kudos
MathewCoyle
Frequent Contributor
It really shouldn't even be taking that long, unless your strings are really long or you are dealing with a slow computer. Did you try it in a file geodatabase?
0 Kudos