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)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 13, 2015 at 11:58
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) 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)
answered Nov 13, 2015 at 12:59
1
  • That is exactly what I have been looking for. Thanks for the setValue example! Commented Nov 13, 2015 at 14:46
0

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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.