1

I am trying to build a simple tool that concatenate values from two fields, and recording it in newly created field - everything within the same table. This tool should work with any shapefile, concatenate any type of fields and create a field (with name chosen by user). My problem is how to pass value of 'new_field_name" in to row.getValue() (second last row of the code). Or any other ways of doing the same?

import time, arcpy
startTime = time.clock()

print "Start"

shapefile = arcpy.GetParameterAsText(0)
field_name_1 = arcpy.GetParameterAsText(1)
field_name_2 = arcpy.GetParameterAsText(2)
new_field_name = arcpy.GetParameterAsText(3)
new_field_length = arcpy.GetParameterAsText(4)
separator = arcpy.GetParameterAsText(5)
arcpy.env.workspace = shapefile
arcpy.env.overwriteOutput = True

arcpy.AddField_management(shapefile, new_field_name, "Text", "", "", new_field_length)

cur = arcpy.UpdateCursor(shapefile)
for row in cur:
    row.getValue() = (str(row.getValue(field_name_1)) + separator + str(row.getValue(field_name_2)))
    cur.updateRow(row)
1
  • I am using arcgis 10.0 so I am not sure if I can use da.UpdateCursor(s) ...just to give you more details. script is working fine once I replace row.getValue() with row.(here is hard coded new field name). But I want to have flexibility here, that is why I want to use a value given by user in arcpy.GetParameterAsText(3)
    – ChrisL
    Commented Nov 13, 2015 at 13:03

2 Answers 2

2

If you need to use the plain UpdateCursor, use setValue instead of getValue

cur = arcpy.UpdateCursor(shapefile)
for row in cur:
    newValue = str(row.getValue(field_name_1)) + separator + str(row.getValue(field_name_2))
    row.setValue(new_field_name, newValue)
    cur.updateRow(row)

Cursor syntax gets a little simpler when you can use the da.UpdateCursor:

with arcpy.da.UpdateCursor(shapefile, [field_name_1, field_name_2, new_field_name]) as cursor:
    for row in cursor:
        newValue = str(row[0]) + separator + str(row[1])
        row[2] = newValue
        cursor.updateRow(row)
1
  • That is exactly what I have been looking for. Thanks for the setValue example!
    – ChrisL
    Commented Nov 13, 2015 at 14:46
0

Try row.setValue for the newly added column in your update cursor.

1
  • at the end that was the setValue, but I couldnt make it work :/
    – ChrisL
    Commented Nov 13, 2015 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.